LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to extract ascii separated values in a text file? (https://www.linuxquestions.org/questions/linux-general-1/how-to-extract-ascii-separated-values-in-a-text-file-925562/)

depam 01-24-2012 10:34 PM

how to extract ascii separated values in a text file?
 
Hi,

I have one file consisting of lines which has some info separated by "|". I want to grep the file and provides me a list of records on the 3rd, 4th entry on a single line. How will this be possible? I am guessing should be using "sed" but not an expert. Also wondering if you can provide me a good reference for logs and values manipulation. Thanks.

flamelord 01-24-2012 11:59 PM

Do you mean the 3rd and 4th fields on the records that get through grep?

That could be achieved with:
Code:

grep <whatever> | cut -d'|' -f3,4
it could also be down in awk with something like:

Code:

awk -F'|' '/pattern/ {$3, $4}'
sed could do it too, but I think that would be a little more complicated.

chrism01 01-25-2012 12:08 AM

Given file t.t
Code:

a|b|c|d|e
q|w|e|r|t

then
Code:

awk '{print $3,$4}' FS="|" t.t
c d
e r

HTH
:)

depam 01-26-2012 10:31 PM

Thanks guys...Now another complicated thing is that inside the file, there are few texts and this information is in the middle of the line.

Example:

some text some text [A|B|C|D] some text some text

I want to check the pattern "II" in that line and output only those lines with that content and if possible only those starting with "[" and ending with "]".

Thanks.

flamelord 01-27-2012 12:43 AM

I think something like

Code:

sed -e '/II/ s/.*\[\(.*\)\].*/\1/p' -n <file>
would work


All times are GMT -5. The time now is 11:19 PM.