Awk concatenation in different lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk concatenation in different lines
# 1  
Old 06-28-2010
Awk concatenation in different lines

Hi All

I have the data as

id-number 01
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0

id-number 02
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0

.
.

I want the out put as
id-number 01 name-id x0 input-id x0 output-id x0
id-number 01 name-id x0 input-id x0 output-id x0
id-number 01 name-id x0 input-id x0 output-id x0

id-number 02 name-id x0 input-id x0 output-id x0
id-number 02 name-id x0 input-id x0 output-id x0
id-number 02 name-id x0 input-id x0 output-id x0

i want this using awk/sed

we can delimit the file with text "id-number"
Thanks
# 2  
Old 06-28-2010
Welcome to the forum. Here is one possibility:
Code:
awk '/id-number/{h=$0" ";next} NF{printf h}1' infile

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-28-2010
Thanks dude Scrutinizer

ur script worked perfect

i forgot to some information in the post like
the data can also as

id-number 01
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0

id-number 02
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0

id-number 03

id-number 04
name-id x0 input-id x0 output-id x0
name-id x0 input-id x0 output-id x0
.
.
id-number 03 doesn't have any info for itself but it should not be deleted the
the output must be like this


id-number 01 name-id x0 input-id x0 output-id x0
id-number 01 name-id x0 input-id x0 output-id x0
id-number 01 name-id x0 input-id x0 output-id x0

id-number 02 name-id x0 input-id x0 output-id x0
id-number 02 name-id x0 input-id x0 output-id x0
id-number 02 name-id x0 input-id x0 output-id x0

id-number 03

id-number 04 name-id x0 input-id x0 output-id x0
id-number 04 name-id x0 input-id x0 output-id x0

thanks
# 4  
Old 06-29-2010
Hi



Code:
 awk '/id-number/{h=$0" ";i=0;next} NF{printf h;print;i++;next;}{if (i==0) {printf h;print;}}1' infile


Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 06-29-2010
wonderful GURU *****
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenation lines based on first field of the lines

Hello All, This is to request some assistance on the issue that I encountered until recently. Problem is: I have a pipe delimited file in which some lines/records are broken. Now, I have to join/concatenate broken lines in the file to form actual record to make sure that the count of records... (8 Replies)
Discussion started by: svks1985
8 Replies

2. Shell Programming and Scripting

awk concatenation issue - SQL generation

Greetings Experts, I have an excel file and I am unable to read it directly into awk (contains , " etc); So, I cleansed and copied the data into notepad. I need to generate a script that generates the SQL. Requirement: 1. Filter and select only the data that has the "mapping" as "direct"... (4 Replies)
Discussion started by: chill3chee
4 Replies

3. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

4. Shell Programming and Scripting

Issue in Concatenation/Joining of lines in a dynamically generated file

Hi, I have a file containing many records delimited by pipe (|). Each record should contain 17 columnns/fields. there are some fields having fields less than 17.So i am extracting those records to a file using the below command awk 'BEGIN {FS="|"} NF !=17 {print}' feedfile.txt... (8 Replies)
Discussion started by: TomG
8 Replies

5. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

6. Shell Programming and Scripting

Concatenation in awk not working

Hello I want to achieve the following. However the concatenation is not working mv `ls -ltr *myfile*.log|awk '{print $9}'` `ls -ltr *myfile*.log|awk '{print `date +'%d%m%y%k%M%S'` $9}'` I tried awk '{x=`date +'%d%m%y%k%M%S'` print $x "" $9}' awk '{x=`date +'%d%m%y%k%M%S'`... (2 Replies)
Discussion started by: Chetanz
2 Replies

7. UNIX for Dummies Questions & Answers

awk for concatenation of column values

Hello, I have a table as shown below. I want to concatenate values in col2 and col3 based on a value in col4. 1 X Y A 3 Y Z B 4 A W B 5 T W A If col4 is A, then I want to concatenate col3 with itself. Otherwise it should concateneate col2 with col3. 1 X Y YY 3 Y Z YZ... (10 Replies)
Discussion started by: Gussifinknottle
10 Replies

8. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

9. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

10. Shell Programming and Scripting

cannot get logic for concatenation awk

Hello friends, I have a problem in printing an array.. Example if my array line contains 4 elements like following line=0002 , line=202200, line=200002, line= 300313 Now one = sprintf line line line line will concatenate my whole array to one. But I am not sure about the... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question