Move lines above a line onto one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move lines above a line onto one line
# 1  
Old 07-19-2010
Move lines above a line onto one line

So say I have the file:
Code:
john
london
24
male
========
jane
london
22
female
 
========
mike
23
 
========
Bob

how do i get the information i need on one line as such:

Code:
john london 24 male
======
jane london 22 female
======
mike 23
======
Bob


so using the ===== as some kind of seperator

Last edited by radoulov; 07-19-2010 at 11:42 AM.. Reason: Please use code tags!
# 2  
Old 07-19-2010
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'END { print rec }
/^=/ { 
  print rec; print
  rec = null; next 
  }
{ rec = rec ? rec FS $0 : $0 }
' infile

Or:

Code:
awk 'END { print RS }
{
  printf "%s" ,/^=/ ? RS $0 RS : $0 FS
  }' infile

# 3  
Old 07-19-2010
Quote:
Originally Posted by radoulov
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'END { print rec }
/^=/ { 
  print rec; print
  rec = null; next 
  }
{ rec = rec ? rec FS $0 : $0 }
' infile

Or:

Code:
awk 'END { print RS }
{
  printf "%s" ,/^=/ ? RS $0 RS : $0 FS
  }' infile

Not too sure what you mean about Solaris. I'm using bash scripting, could you enlighten me anymore please?
# 4  
Old 07-19-2010
Usually you use shell scripting to invoke external commands. awk is such an external command. It's quite powerful tool for text processing. When I tell you to use gawk, nawk etc, I'm just saying not to use the old awk interpreter found in /usr/bin on Solaris, because it's too old and broken. So, if you're on Solaris, you should invoke the command I posted not as awk ..., but as nawk ... (or gawk etc).

Hope this helps.
This User Gave Thanks to radoulov For This Post:
# 5  
Old 07-19-2010
Code:
awk '/^=/ {printf "\n"$0"\n";next} {printf $0 FS}' urfile

# 6  
Old 07-19-2010
Code:
#!/bin/bash

while read -r LINE
do
  case "$LINE" in
   ==*) printf "\n$LINE\n";;
   *) printf "%s " "$LINE"
  esac
done <"file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

3. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

6. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

7. Shell Programming and Scripting

How to calculate mean in AWK? line by line several files, thousands of lines

I'm kinda stuck on this one, I have 7 files with 30.000 lines/file like this 050 0.023 0.504336 050 0.024 0.529521 050 0.025 0.538908 050 0.026 0.537035 I want to find the mean line by line of the third column from the files named like this: Stat-f-1.dat .... Stat-f-7.dat Stat-s-1.dat... (8 Replies)
Discussion started by: AriasFco
8 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies
Login or Register to Ask a Question