I've read a bunch of text files to extract some patterns. I need the line number too but the line number must be removed before the final grep (but can be saved for further processing maybe using variables).
I'll explain my procedure splitting the (long oneliner) command for better understanding:
read file with cat, do some cleaning (remove strange characters & line feeds using sed and tr) and such. Here just an example of many piped cleaning tasks:
cat file | sed 's/,/ /g' | sed '/^$/d'
add line number and tab with nl command & more processing and cleaning
nl -nrz -w4 -s$'\t' | tr '\n\r' ' '
extract the final desired pattern to CSV file
grep -Eio '.{0,0}account number.{0,15}' >> account_list.csv
The issue is I need the line number from step 2 to the very same CSV (other column no matter the order) using the SAME ONE LINE COMMAND (no while or loop allowed) but no success so far.
[EDITED for better understanding] Take into account the line number I need is the original one prior to file cleaning. This cleaning process sometimes deletes some paragraphs. Imagine a file with a thousand lines, after processing i got one hundred. New line numbering is wrong. [end edit]
Sample imput after some processing:
0123 the first account number 2345356432 must be used
0345 take it just for billing purposes, not any other.
0657 Meanwhile the second account number 8623525534
0987 user is blocked until the issue is solved with
The desired oputput would be:
2345356432; 0123
8623525534; 0657
or
0123; 2345356432
0657; 8623525534
any hint would be much appreciated
sed -rn 's/ .*account number (\S{,15}) *.*/; \1/p'
but if the string account … is splitted in 2 lines? Which number you'l need? – Costas Aug 18 '16 at 10:23account
s in account_list.csv. What kind of "break the program flow" do you mean? – Costas Aug 18 '16 at 10:59