Combine multiple lines in file based on specific field


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine multiple lines in file based on specific field
# 1  
Combine multiple lines in file based on specific field

Hi,

I have an issue to combine multiple lines of a file. I have records as below.
Fields are delimited by TAB. Each lines are ending with a new line char (\n)

Input
--------
Code:
ABC 123456 abcde 987
890456 7890 xyz
ght gtuv 
ABC 5tyin 1234 789
ghty kuio
ABC ghty jind 1234 
678 ght

I have to combine these lines based on first field. Whenever ABC comes a first field it should a new line. Delimiter should be retained as TAB.

My output should be
--------------------

Code:
ABC 123456 abcde 987 890456 7890 xyz ght gtuv
ABC 5tyin 1234 789 ghty kuio
ABC ghty jind 1234 678 ght

Please help me to resolve this.

Thanks,
Ratheesh

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.
# 2  
Try:
Code:
perl -lp0e 's/^ABC/\x00ABC/gm;s/\n/\t/g;s/\x0/\n/g' input > output

# 3  
Code:
awk 'END { 
  if (r) 
    print r 
    }
/^ABC/ && r { 
  print r
  r = x 
  }
{ 
  r = r ? r $0 : $0 
  }' infile

# 4  
Code:
paste -sd'\t' inputfile|sed 's/        ABC/\
ABC/g'

Use the Tab key between "/" and "ABC".
# 5  
Another one:

Code:
sed -e :a -e '$!N;/\nABC/!s/\n//;ta' -e 'P;D' infile

# 6  
Not working as expected

Thanks all for your quick reply. But these codes are not working as I expected.
All are writing the input records to output as it is.
Solution from elixir_sinari prefixed a TAB for each record except 1st.
Code:
ABC 123456 abcde 987
       890456 7890 xyz
       ght gtuv 
       ABC 5tyin 1234 789
       ghty kuio
       ABC ghty jind 1234 
       678 ght

Please help me.
Moderator's Comments:
Mod Comment Code tags for code please.
# 7  
If you have GNU awk:

Code:
$ awk -v RS="ABC" '{ $1="ABC " $1 } NF>1' data

ABC 123456 abcde 987 890456 7890 xyz ght gtuv
ABC 5tyin 1234 789 ghty kuio
ABC ghty jind 1234 678 ght

$

This User Gave Thanks to Corona688 For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #9
Difficulty: Easy
A CD-ROM is a type of optical disc that originated in 1982 in South Korea.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

2. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

3. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

5. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

6. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

7. Shell Programming and Scripting

File Parsing based on a character in a specific field

Hi All, I'm having a hard time finding a starting point for my issue. I have a 30k line file (fspsec.txt) that I would like to parse into smaller files based on any character existing in field 1. ACCOUNTANT LEVEL 1 (ACCT.ACCOUNTANT) OPERATORS: DOEJO (418) TOOLS: Branch Maintenance ... (2 Replies)
Discussion started by: aahlrich
2 Replies

8. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

9. Shell Programming and Scripting

Split a file into multiple files based on field value

Hi, I've one requirement. I have to split one comma delimited file into multiple files based on one of the column values. How can I achieve this Unix Here is the sample data. In this case I have split the files based on date column(c4) Input file c1,c2,c3,c4,c5... (1 Reply)
Discussion started by: manasvi24
1 Replies

10. Shell Programming and Scripting

add lines automatically based on a field on another file

hello I have a number of lines that need to be added at the end of a file each time I add a field in another file (let's name it file2) file2 has this format: filed1:field2:path1:path2:path3:path... Whenever I add a path field, I should add to file1 these lines: <Location path1>... (0 Replies)
Discussion started by: melanie_pfefer
0 Replies

Featured Tech Videos