LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-24-2012, 10:34 PM   #1
depam
Member
 
Registered: Sep 2005
Posts: 829

Rep: Reputation: 30
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.
 
Old 01-24-2012, 11:59 PM   #2
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
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.
 
Old 01-25-2012, 12:08 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,301

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 01-26-2012, 10:31 PM   #4
depam
Member
 
Registered: Sep 2005
Posts: 829

Original Poster
Rep: Reputation: 30
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.
 
Old 01-27-2012, 12:43 AM   #5
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
I think something like

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] splitting of comma separated values file (horizontal) into a list (vertical ) list Donoughue Linux - Newbie 3 10-20-2011 01:18 PM
How to convert encoding between ASCII text and ASCII English text astanton Linux - General 2 08-03-2011 03:55 PM
[SOLVED] Variables in text file, how to get their values when printing out the text? idaham Linux - General 2 04-14-2010 03:28 AM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
Extract certain text info from text file xmrkite Linux - Software 30 02-26-2008 11:06 AM


All times are GMT -5. The time now is 04:32 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration