LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 09-09-2011, 12:08 PM   #1
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
deleting fields in lines that have more fields than the average


I have this problem i have been trying with awk:
i have this data:

Code:
1 2 a b c d
    e f g h
    i j k l
3 4 m n o p
    q r s t
and i want to get this

Code:
a b c d
e f g h
i j k l
m n o p
q r s t
any idea guys, i wanted to compare the number of fields between lines but i never got a good code in google, i am still googlin BTW

Last edited by patolfo; 09-09-2011 at 12:09 PM.
 
Old 09-09-2011, 12:18 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Is awk required?

Using cut:
Code:
cut -c 5- infile
Hope this helps.
 
Old 09-09-2011, 02:27 PM   #3
lrtward
Member
 
Registered: Feb 2011
Distribution: CentOS, Ubuntu
Posts: 97

Rep: Reputation: 9
Lightbulb

If awk is required:
Code:
# cat file
1 2 a b c d
    e f g h
    i j k l
3 4 m n o p
    q r s t

# awk '{ print $(NF-3), $(NF-2), $(NF-1), $NF }' file
a b c d
e f g h
i j k l
m n o p
q r s t
Though I must say the previous solution by druuna is better if awk is not necessary.

Last edited by lrtward; 09-09-2011 at 02:28 PM. Reason: misspelled druuna's name
 
Old 09-10-2011, 02:24 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
Well if awk less than 4.0:
Code:
awk --re-interval 'sub(/^.{4}/,"")' file
Version 4 and above the switch is not required as intervals are native. I agree the cut is the better tool in this case.
 
1 members found this post helpful.
Old 09-14-2011, 11:03 AM   #5
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
Rep: Reputation: 15
thanks a bunch guys :)

grail your code worked real good

adn i doe sno thave to be awk, bu ti prefer it, as i am piping data using only awk commands so far.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cutting fields from lines with multiple spaces 915086731 Linux - Software 6 09-07-2011 09:51 AM
Average from values of fields sebelk Programming 3 08-14-2011 07:40 AM
Deleting lines based on comparing fields..... OldGaf Programming 2 02-22-2008 07:04 AM
AWK - why fields go to seperate lines? korhan Linux - Newbie 2 03-01-2007 03:21 PM
Unique lines based on specific fields. carl.waldbieser Programming 6 08-21-2005 02:26 PM


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

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