LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-30-2010, 06:12 AM   #1
Mallardle
LQ Newbie
 
Registered: Aug 2010
Location: Aberdeen
Distribution: RH4 & Xubuntu
Posts: 3

Rep: Reputation: 0
SED or AWK - remove every 4 of 5 new lines


Hi,

I have a file that looks like this:

1
2 3 4 5 6 7 8 9
10 11 12 13 14 15
16 17 18 19 20 21
22 23 24
1
2 3 4 5 6 7 8 9
10 11 12 13 14 15
16 17 18 19 20 21
22 23 24
1
2 3 4 5 6 7 8 9
10 11 12 13 14 15
16 17 18 19 20 21
22 23 24
...

I would like to reformat it to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
...

Is there a nifty awk/sed one-liner to do this operation?

Cheers,
Nick
 
Old 08-30-2010, 06:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Something like:
Code:
awk 'NR>1 && /^1$/{print ""}{printf $0" "}END{print ""}' file
 
Old 08-30-2010, 06:45 AM   #3
Mallardle
LQ Newbie
 
Registered: Aug 2010
Location: Aberdeen
Distribution: RH4 & Xubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks very much Grail,

That worked perfectly in the example, but I have a follow up question. I have a file in a similar layout, but with different numbers and with irregular spaces between records and at the start of each line:

982.5760
982.5760 -982.5760 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
-999.2500 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
15.8611 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
-999.2500 -999.2500 -999.2500
982.6268
982.6268 -982.6268 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
-999.2500 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
15.6731 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
-999.2500 -999.2500 -999.2500
982.6776
982.6776 -982.6776 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
-999.2500 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
15.6087 -999.2500 -999.2500 -999.2500 -999.2500 -999.2500
-999.2500 -999.2500 -999.2500

Is there a way I can adapt your solution for this, and what does /^1$/ do in awk?

Many Thanks,
Nick
 
Old 08-30-2010, 06:53 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can also do it with pure bash

Code:
#!/bin/bash
exec 4<"file"
while read -r a b <&4
do
  case "$b" in
   "") printf "$a ";;
   *) echo "$a $b";;
  esac
done
exec 4<&-
awk if you insist
Code:
awk 'NF==1{printf "%s ",$0;next}1' file
 
Old 08-30-2010, 07:07 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
what does /^1$/ do in awk?
Based on original input this looked for the line that contains a '1' and nothing else.

You can use above bash or a combo of mine and awk above gets your results:
Code:
awk 'NR>1 && NF==1{print ""}{printf $0" "}END{print ""}' file
 
Old 08-30-2010, 07:21 AM   #6
Mallardle
LQ Newbie
 
Registered: Aug 2010
Location: Aberdeen
Distribution: RH4 & Xubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks Grail and Ghostdog.

Both solutions worked a treat.

Nick
 
Old 08-30-2010, 07:44 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Not sure if your requirements had changed but the bash doesn't quite work as is.
Slight change gets the same as my awk script:
Code:
#!/bin/bash

count=0
exec 4<"file"

while read -r a b 
do
  case "$b" in
   "") ((count++)) && out="\n$a" || out="$a";;
   *) out=" $a $b";;
  esac
	printf "$out"
done<&4
exec 4<&-

echo
 
  


Reply



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
[SOLVED] sed or awk question, find lines with signs and number them Krzysztow Linux - Newbie 14 05-18-2010 10:23 AM
Inserting Multiple Lines (with newline) using sed or awk hal8000b Programming 1 03-08-2009 05:21 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
find awk sed.. something along these lines citrus Linux - General 1 08-21-2006 03:04 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

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

All times are GMT -5. The time now is 02:59 AM.

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