Extract information from a.txt file

Asked

Viewed 494 times

3

I have a.txt file with the following information.

31.812:9.862 179.52:6.847 315.015:9.135 462.305:6.276
103.875:4.122 176.064:3.593 196.7:4.407 246.599:4.423

And I need to extract that information

9.862 6.847 9.135 6.276 
4.122 3.593 4.407 4.407

( that is the values that are between the : and space )

  • The archive txt has as default this formatting?

3 answers

5

You can use the command tr to replace the : for and then use the command cut to select only the even columns.

Ex.:

cat arquivo.txt | tr ':' ' ' | cut -d' ' -f2,4,6,8

TR: replaces all the colon for space

CUT:

  • -d: sets the character space as field delimiter
  • -f: Select only fields 2, 4, 6 and 8 which are the numbers you are interested in.

txt file.

31.812:9.862 179.52:6.847 315.015:9.135 462.305:6.276
103.875:4.122 176.064:3.593 196.7:4.407 246.599:4.423

output

9.862 6.847 9.135 6.276
4.122 3.593 4.407 4.423
  • But if he selects fields with 6 he will end up picking up information he does not want, for example: 103.875 or not?

  • So @R.Santos, the question doesn’t make it clear if that pattern is for all the file it needs.. My solution is fast but applies to files with 4 "elements" per line. As the question does not clarify this I left this answer as an alternative to Regex. E as I do not understand anything of awk that’s the solution I found..

  • Okay, just so we’re clear, I questioned him about it to clear the doubt

  • I edited the question to add an example.

  • 1

    It is good to make clear that my solution only takes 4 per line, if you have more ignores.

  • Right, so I questioned him regarding the file pattern txt

Show 1 more comment

3

Use regular expressions to perform the required file extraction .txt, to get what you need I used the following expression: :\d. d{3}

Test here

  • Perfect quote using the expressions, fast and practical.

2

sed -r 's/[^ ]+:([^ ]+)/\1/g' arquivo.txt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.