sps
sps

Reputation: 2730

How to edit a file with shell scripting

I have a file containing thousands of lines like this:

0x7f29139ec6b3: W 0x7fff06bbf0a8
0x7f29139f0010: W 0x7fff06bbf0a0
0x7f29139f0014: W 0x7fff06bbf098
0x7f29139f0016: W 0x7fff06bbf090
0x7f29139f0036: R 0x7f2913c0db80

I want to make a new file which contains only the second hex number on each line (the part marked in bold above)

I have to put all these hex numbers in an array in a C program. So I am trying to make a file with only the hex numbers on the right hand side, so that my C program can use the fscanf function to read these numbers from the modified file.

I guess we can use some shell script to make a file containing those hex numbers? grep or something?

Upvotes: 2

Views: 75

Answers (4)

ruakh
ruakh

Reputation: 183564

Since it seems that you always want to select the third field, the simplest approach is to use cut:

cut -d ' ' -f 3 file

or awk:

awk '{print $3}' file

Upvotes: 0

Mark Reed
Mark Reed

Reputation: 95375

You can run a command on the file that will create a new file in the format you want: somecommand <oldfile >newfile. That will leave the original file intact and create a new one for you to feed to your C program.

As to what somecommand should be, you have multiple options. The easiest is probably awk:

awk '{print $NF}'

But you can also do it with sed or grep or perl or cut ... see other answers for an embarrassment of choices.

Upvotes: 0

anubhava
anubhava

Reputation: 786091

You can use grep -oP command:

grep -oP ' \K0x[a-fA-F0-9]*' file
0x7fff06bbf0a8
0x7fff06bbf0a0
0x7fff06bbf098
0x7fff06bbf090
0x7f2913c0db80

Upvotes: 0

dev0z
dev0z

Reputation: 2415

You can use sed and edit inplace. For matching "R" or any other char use

sed -i "s/.*:..//g" file

cat file
0x7fff06bbf0a8

0x7fff06bbf0a0

0x7fff06bbf098

0x7fff06bbf090

Upvotes: 1

Related Questions