3

I have long file with the following list:

/drivers/isdn/hardware/eicon/message.c//add_b1()
/drivers/media/video/saa7134/saa7134-dvb.c//dvb_init()
/sound/pci/ac97/ac97_codec.c//snd_ac97_mixer_build()
/drivers/s390/char/tape_34xx.c//tape_34xx_unit_check()
(PROBLEM)/drivers/video/sis/init301.c//SiS_GetCRT2Data301()
/drivers/scsi/sg.c//sg_ioctl()
/fs/ntfs/file.c//ntfs_prepare_pages_for_non_resident_write()
/drivers/net/tg3.c//tg3_reset_hw()
/arch/cris/arch-v32/drivers/cryptocop.c//cryptocop_setup_dma_list()
/drivers/media/video/pvrusb2/pvrusb2-v4l2.c//pvr2_v4l2_do_ioctl()
/drivers/video/aty/atyfb_base.c//aty_init()
/block/compat_ioctl.c//compat_blkdev_driver_ioctl()
....

It contains all the functions in the kernel code. The notation is file//function.

I want to copy some 100 files from the kernel directory to another directory, so I want to strip every line from the function name, leaving just the filename.

It's super-easy in python, any idea how to write a 1-liner in the bash prompt that does the trick?

Thanks,

Udi

0

4 Answers 4

12
cat "func_list" | sed "s#//.*##" > "file_list"

Didn't run it :)

Sign up to request clarification or add additional context in comments.

2 Comments

BTW +1 for the courage of publishing it without testing it!
@Dennis Yeah, I know. I always forget backup syntax and I'm usually trying to give non destructive commands, especially if I don't run them beforehand :)
6

You can use pure Bash:

while read -r line; do echo "${line%//*}"; done < funclist.txt

Edit:

The syntax of the echo command is doing the same thing as the sed command in Eugene's answer: deleting the "//" and everything that comes after.

Broken down:

"echo ${line}" is the same as "echo $line"
the "%" deletes the pattern that follows it if it matches the trailing portion of the parameter
"%" makes the shortest possible match, "%%" makes the longest possible
"//*" is the pattern to match, "*" is similar to sed's ".*"

See the Parameter Expansion section of the Bash man page for more information, including:

  • using ${parameter#word} for matching the beginning of a parameter
  • ${parameter/pattern/string} to do sed-style replacements
  • ${parameter:offset:length} to retrieve substrings
  • etc.

1 Comment

Can you please explain the syntax within rhe "echo", for future generations reading your answer?
4

here's a one liner in (g)awk

awk -F"//" '{print $1}' file

1 Comment

What's the major difference between awk and sed?
0

Here's one using cut and rev

cat file | rev | cut -d'/' -f2-| rev

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.