LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-29-2017, 12:33 PM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
issue with awk


Hi Team
Am new to unix/linus

I am trying to use awk to pull certain cols but having trouble.

Below is the data in my faile.

Code:
IDNU      KEY_NAM    SIZE
--------------------------------
DB901     TAB_A      9.8 GB
DB890     TAB _A_1   1.1 GB
DB797     T   _A _1  0.1 GB
cat file.log| awk '{ print $1";"$2}'

am only getting TAB_A, TAB, T but missing others in column 2

i tried using sed 's/\s\s * / /g' but it only works for TAB _A_1 but not others

any other better idea to pull whole column 2
 
Old 08-29-2017, 12:36 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Are the columns separated by tabs?

Code:
awk '{ print $1,$2; }' FS="\t" OFS=";" file.log
 
Old 08-29-2017, 12:44 PM   #3
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Nope they are not tab separated.
 
Old 08-29-2017, 01:15 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora 33
Posts: 3,576

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
I think you will just have to count columns.

Code:
awk '{c2=substr($0,11,10); print $1 ";" c2}' foo
IDNU;KEY_NAM   
--------------------------------;----------
DB901;TAB_A     
DB890;TAB _A_1  
DB797;T   _A _1
 
Old 08-29-2017, 01:15 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
You'll have to split or adjust the columns based on their absolute position then.

If you use sed first, then it could be like this:

Code:
sed -E -e 's/^(.{9}) (.{10}) /\1\t\2\t/; s/ +\t/\t/g;' file.txt | awk . . . FS="\t"
Or you could do it all in awk somewhat differently. Probably the substr() function would be of use there. Or if you have Gnu awk (gawk) you can use the FIELDWIDTHS variable.

Which version of awk do you have?
 
Old 08-29-2017, 02:03 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,194

Rep: Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040
If you are using Gawk (and you should), there is a solution for exactly that:
https://www.gnu.org/software/gawk/ma...tant-Size.html

jlinkels
 
Old 08-29-2017, 03:16 PM   #7
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Thanks all. sed worked out for me
 
Old 08-29-2017, 11:34 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
No problem.

Can we still ask which version of awk you have? There is an all-awk solution or two also but the optimal way for that depends on which version you have.
 
Old 08-30-2017, 03:19 AM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,637

Rep: Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205
To just get column 2, why not 'cut -b11-20'?
 
Old 08-30-2017, 05:30 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Quote:
Originally Posted by allend View Post
To just get column 2, why not 'cut -b11-20'?
That would work well unless there is some constraint to use only awk. If gawk is available, then the following would be a solution:

Code:
gawk '{print $1, $2, $3;}' OFS=";" FIELDWIDTHS="10 11 10" inputfile.txt
But the FIELDWIDTHS variable is not available for other versions of awk.
 
Old 08-30-2017, 06:52 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,746

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Also consider unexpand to convert to tab-separated fields.
Code:
man unexpand
 
Old 08-30-2017, 12:29 PM   #12
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
awk - 3.1.7
 
Old 08-30-2017, 12:43 PM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Thanks. But is it mawk, gawk, nawk, or old awk? Only gawk has the FIELDWIDTHS built-in variable which can split columns based on width.
 
Old 09-06-2017, 11:04 AM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,746

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
With unexpand and the input from post#1 you can convert space to TAB at certain positions,
then pipe to awk with a field separator set to TAB
Code:
unexpand -t 10,21 file.log | awk -F"\t" '{print $1,$2}'
IDNU KEY_NAM
-------------------------------- 
DB901 TAB_A
DB890 TAB _A_1
DB797 T   _A _1
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk file redirect issue sumathi_subramaniam Linux - Newbie 6 11-04-2013 07:39 AM
awk issue with adding formula columns? cusvenus Linux - Newbie 3 06-15-2013 03:06 AM
awk command issue vicky007aggrwal Linux - Newbie 4 10-31-2012 01:22 PM
Awk issue !! PMP Programming 8 06-08-2010 02:20 AM
awk variable issue cdestiny Linux - General 5 03-10-2010 11:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:52 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