LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-19-2014, 11:52 AM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
Thumbs up get the data from pipe delimited file using awk


I want to extract the data from 1st and 2nd column from a pipe "|" delimited text file and keep it in two separate variables.

How can I do this using awk? If the file was space separated I can use just print $1 for first column and print $2 for second column.

But how can I do this when the file is pipe separated?
Suppose the file is:

Quote:
HYDROGEN|H|1
OXYGEN|O|8
NITROGEN|N|7
CARBON|C|6
 
Old 08-19-2014, 11:56 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
http://stackoverflow.com/questions/2...separated-file
 
Old 08-19-2014, 12:58 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This question and others like it have been answered many times; please, take a look here as an example: http://www.linuxquestions.org/questi...tching-900345/
 
Old 08-20-2014, 03:28 PM   #4
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
Quote:
Originally Posted by jone kim View Post
I want to extract the data from 1st and 2nd column from a pipe "|" delimited text file and keep it in two separate variables.

How can I do this using awk?
Code:
awk -F\|  '{ var1=$1; var2=$2; print var1, var2 }' file.txt
Or
Code:
cat file.txt | awk -F\| '{ var1=$1; var2=$2; print var1, var2 }'

Last edited by initramfs; 08-20-2014 at 03:35 PM.
 
Old 08-20-2014, 06:52 PM   #5
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
Hi jone kim

I'm not sure what you wanted, in my first code above, I assumed you wanted to extract the columns into separate variables and then use these variables with awk.

In this example, we use awk to store the variables and then have other programs use the variables
Code:
#!/bin/bash 

while read line
do
	var1=$(awk -F\| '{ print $1 }' <<< "$line")
	var2=$(awk -F\| '{ print $2 }'  <<< "$line")
	echo "$var1 $var2"
done < file.txt
In this simple example the echo command is using the variables.

Last edited by initramfs; 08-20-2014 at 06:55 PM.
 
Old 08-20-2014, 10:49 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,987
Blog Entries: 4

Rep: Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037
awk is designed to make things like this very easy ... for instance the "FS" (Field Separator) and "RS" (Record Separator) variables.

Please step back from the immediate requirement and spend just a couple hours "really surfing-the-web" about this amazingly useful tool. Maybe spend a half-day on it. You'll be very, very glad that you did. This is one of the most-powerful tools in your daily arsenal. It will profit you to learn to use it well.
 
Old 08-21-2014, 01:56 PM   #7
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
This is one of the most-powerful tools in your daily arsenal. It will profit you to learn to use it well.
I agree. I know the basics of awk and sed and I barely touch the power of these two commands. I hope my suggested codes is what the OP is seeking.

Awk and sed are so powerful with many features that O'Reily have books dedicated on the subject.

Last edited by initramfs; 08-21-2014 at 02:36 PM.
 
  


Reply

Tags
awk


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
How to ignore Pipe in Pipe delimited file? rohit_shinez Programming 29 08-14-2013 12:53 AM
[SOLVED] sed or awk to remove $8 in a | delimited file if non-numerical value exists BludGeonT Programming 5 06-22-2013 12:57 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 01:17 PM
[SOLVED] Move line of pipe delimited flat file if field 27 = sold Radical-Rick Programming 4 09-27-2010 07:59 PM
Help needed in removing intermediate segments from a pipe delimited segment file naren_0101bits Programming 12 12-03-2007 11:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:12 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration