Print multiple lines on one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print multiple lines on one line
# 1  
Old 09-11-2015
Print multiple lines on one line

Hi All,

I have a file looks like:

Code:
rst:singh:99.0.20-X86 2 rst:ACSI_SIN_SERVICES
rst:singh:99.0.20-X86 2 rst:ACSI_BIS

I want to wrap 3rd col in one line and add variable value at start and ending of line and I wrote command:

Code:
cat file | awk '{print $3}' | xargs > command.txt
sed -e "\$s/^/$var2 /" command.txt > out.txt
sed -e "\$s/$/ $var3/" out.txt > command.txt

working fine and getting:
Code:
"var2 value" rst:ACSI_SIN_SERVICES rst:ACSI_BIS "var3 value"

but in other file which looks like:

Code:
com.rst.tom:hinje:99.0.30-X86   1          com.rst.tom:ACSI_SIN                                           [94.0.5]
com.rst.tom:mar:99.0.30-X86   1          com.rst.tom:PUNE_WAK_Mangement                             [94.0.6]

in this case its not running
Please help

Last edited by rakeshtomar82; 09-11-2015 at 11:17 AM.. Reason: code tags
# 2  
Old 09-11-2015
Hello rakeshtomar82,

As you didn't show us the expected output so based on my understanding following may help you in same, where my logic says you need to get the last column in a single line with separated with :.
Input_file1:
Code:
cat test435
rst:singh:99.0.20-X86 2 rst:ACSI_SIN_SERVICES
rst:singh:99.0.20-X86 2 rst:ACSI_BIS

Code for above Input_file:
Code:
awk -F":" '{A=A?A OFS $NF:$NF} END{print A}' OFS=":" test435
As follows is output:
ACSI_SIN_SERVICES:ACSI_BIS

2nd Input_file:
Code:
cat test436
com.rst.tom:hinje:99.0.30-X86 1 com.rst.tom:ACSI_SIN [94.0.5]
com.rst.tom:mar:99.0.30-X86 1 com.rst.tom:PUNE_WAK_Mangement [94.0.6]

Same code for above Input_file:
Code:
awk -F":" '{A=A?A OFS $NF:$NF} END{print A}' OFS=":" test436
Output is as follows:
ACSI_SIN [94.0.5]:PUNE_WAK_Mangement [94.0.6]

Thanks,
R. Singh
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. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

4. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

5. 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

6. Shell Programming and Scripting

Print the first line for each multiple lines

Hi all, i need help to extract each first line from multiple lines occurrences based on different patterns (name) starting from the fourth lines like follows:- // header 1 header 2 header 3 // no acc name score rank //... (2 Replies)
Discussion started by: redse171
2 Replies

7. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

8. Shell Programming and Scripting

(awk?) print multiple lines on one line

I have a log file something like ------- report 1 ------- date 27/01/13 time 08:00 records 1234 ------- report 2------- date 27/01/13 time 08:00 records 1239 ... I'd like output to show as report 1,date 27/01/13,time 08:00,records 1234 report 2,date 27/01/13,time... (6 Replies)
Discussion started by: gefa
6 Replies

9. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

10. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies
Login or Register to Ask a Question