0

I have a text file containing lines of space separated data.

Example:

B  345678  2005-12-21  4174  1  62  11111 16543 1911  786543,45

However the spaces are kind of inconsistant and sometimes I suspect there are some added tabs too. I need to find a was to cut the line exactly before "62" which is a number that always occur in every line and it is somtimes followed by a 5-digit number and then always followed by another 5-digit number. I also add space before 62 to minimixe risk for mismatches.

So far I came up with this:

grep " 62 [1-9][0-9][0-9][0-9][0-9] " file

This will only give the lines where sometimes a 5-digit number shows up. I need a way to grep for 62 followed by an optional 5-digit numbers and then followed by a compulsary 5-digit number.

Can this be done?

/Paul

  • By "cut the line", you mean you want to remove the part before 62? You say "it's sometimes followed by a 5-digit number, and then always followed by another 5-digit number", but isn't that the same the other way around? We can't tell apart the 5-digit numbers without extra information on them. – ilkkachu Nov 15 '19 at 8:10
  • Yes, I want to remove everything before 62 (or one space follewed by 62). The 5-digit numbers are always from 10000 and up to 99999. As I said 62 is followed by a space and a 5-digit number then space and another 5-digit number OR 62 is followed by a inconsistent number of spaces then comes the latter 5-digit number. – Paul Bergström Nov 15 '19 at 8:15
  • Is the "62" always in the 6th column? – AdminBee Nov 15 '19 at 8:16
  • No, it is not. 62 could occur in any line as "62" or " 62" or " 62 ". That is why I need to make sure that the delimiter is the "true" 62 by knowing what comes after. – Paul Bergström Nov 15 '19 at 8:17
  • Please add a sample input file (showing different places "62" can appear, and matching/non-matching lines) as well as the corresponding output you wish to achieve. – glenn jackman Nov 15 '19 at 18:27
0

I need a way to grep for 62 followed by an optional 5-digit numbers and then followed by a compulsary 5-digit number.

That seems to be the same as saying that there's one or two 5-digit numbers after the 62, and then it's enough to just match on the first one. Handling a varying amount of spaces is easy, we can use ␣+, or [[:space:]]+ to include tabs too.

So,

grep -E ' 62 +[1-9][0-9]{4} '

or

grep -E '[[:space:]]62[[:space:]]+[1-9][0-9]{4}[[:space:]]'

This of course will print the entire line. If you want just the part starting from 62, add .* to match the end of the line and -o to print just the matching part:

grep -o -E '[[:space:]]62[[:space:]]+[1-9][0-9]{4}[[:space:]].*'
|improve this answer|||||
  • ilkkachu. Your second grep command did the trick:-) The third one gives bland lines. Thank you so much. – Paul Bergström Nov 15 '19 at 8:38
0

You could use awk for that purpose:

awk '{if (match($0, "(^[[:print:]]*[[:space:]]+)?(62([[:space:]]+[1-9][0-9]{4})?([[:space:]]+[[:print:]]*)?[[:space:]]+[1-9][0-9]{4}([[:space:]]*|[[:space:]]+[[:print:]]*)$)", a)!=0) print a[2];}' file.txt

This will match any line with an isolated 62, either at the beginning or preceded by any number of printable characters and then one or more space characters, then followed by

  • optionally whitespace and a five-digit number
  • optionally any number of printable characters
  • compulsory whitespace and a five-digit number
  • optionally at least one whitespace and any number of printable characters, or whitespace to the end of line

It will print the part starting with the 62 to the end of line if such a pattern was found.

Note that this will not squash the separating whitespaces, so the "inconsistent spaces" will be copied to the output as-is.

|improve this answer|||||
0

... remove everything before 62 (.....)

perl -pe 's/.*?(?= 62 +[1-9]\d{4} )//'

Where:

  • s/.*?...// - means remove everything = substitute by nothing
  • .*?(?= 62) - means something before 62...
|improve this answer|||||
0

I'd suggest

grep -E '\b62([[:blank:]]+[[:digit:]]{5}\b){1,2}'

where \b is a word boundary that will allow "62" at the start of the line or after a non-word character (like whitespace)

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.