Joining lines in a text file using AWK or SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining lines in a text file using AWK or SED
# 1  
Old 02-02-2011
Joining lines in a text file using AWK or SED

Hi All

I'm struggling a bit here Smilie

I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me.

The text file has 4 lines:

Code:
DELL1427 
DOC 
30189342
79

Now bear with me on this one as I'm actually scripting in a batch file using the ported versions of awk and sed (and other unix utils) for Windows.

I've use all these lines (not at the same time) with no luck:

Code:
awk "BEGIN {x = 0} {if (x<2) {printf("%s ",$0)x=x+1} if (x==2) {printf("\n")x = 0} }" %OUTPUT% > %OUTPUT2%
type %OUTPUT% | awk "BEGIN {FS="|";FLDmax=10;}{while (NF < FLDmax || $0 ~ /\|\"$/) {getline record;$0 = $0 record}print $0}"  > %OUTPUT3%
sed "{N;N;/s/\n/ /g}" %OUTPUT% > %OUTPUT2%
sed "$!N;s/\n/ /" %OUTPUT% > %OUTPUT2%
sed -e :a -e N -e "s/\n/ /" -e ta %OUTPUT% > %OUTPUT2%
type %OUTPUT% | tr -d "\n" > %OUTPUT2%

In some cases, the output file has been changed to include spaces at the beginning of each line, other times, it hasn't made any changes to the file - and the complex awk commands don't seem to work at all.


Any ideas where I'm going wrong?
# 2  
Old 02-02-2011
Quote:
Originally Posted by huskie69
Hi All

I'm struggling a bit here Smilie

I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me.

The text file has 4 lines:

Code:
DELL1427 
DOC 
30189342
79

Now bear with me on this one as I'm actually scripting in a batch file using the ported versions of awk and sed (and other unix utils) for Windows.

I've use all these lines (not at the same time) with no luck:

Code:
awk "BEGIN {x = 0} {if (x<2) {printf("%s ",$0)x=x+1} if (x==2) {printf("\n")x = 0} }" %OUTPUT% > %OUTPUT2%
type %OUTPUT% | awk "BEGIN {FS="|";FLDmax=10;}{while (NF < FLDmax || $0 ~ /\|\"$/) {getline record;$0 = $0 record}print $0}"  > %OUTPUT3%
sed "{N;N;/s/\n/ /g}" %OUTPUT% > %OUTPUT2%
sed "$!N;s/\n/ /" %OUTPUT% > %OUTPUT2%
sed -e :a -e N -e "s/\n/ /" -e ta %OUTPUT% > %OUTPUT2%
type %OUTPUT% | tr -d "\n" > %OUTPUT2%

In some cases, the output file has been changed to include spaces at the beginning of each line, other times, it hasn't made any changes to the file - and the complex awk commands don't seem to work at all.


Any ideas where I'm going wrong?
What should be the desired output of the given text file?
# 3  
Old 02-02-2011
Ahhh - I've managed to do it using the 'paste' program bundled with coreutils Smilie

---------- Post updated at 04:41 PM ---------- Previous update was at 01:30 PM ----------

Quote:
Originally Posted by Franklin52
What should be the desired output of the given text file?
Ah - I was missing one vital piece of information for you guys! The desired out put was:

Code:
DELL1427,DOC,30189342,79

I've managed to do it using a combination of paste and sed Smilie
# 4  
Old 02-02-2011
With paste:

Code:
paste -sd, infile

# 5  
Old 02-02-2011
try:

Code:
awk '{a[NR+1]=$1}NR>1{printf a[NR]","}END{print $1}' urfile

or:

Code:
sed ':a;$!N;s/\n/,/;ta' urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
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

Joining broken lines with awk or perl

Hi, I have a huge file with sql broken statements like: PP3697HB @@@@0 <<<<<<Record has been deleted as per PP3697HB>>>>>> FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE RE rc.milf = ur.milf AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur .department = 'IND' AND share = '2' AND... (4 Replies)
Discussion started by: som.nitk
4 Replies

4. UNIX for Advanced & Expert Users

Text formatting - joining lines

I have large file with data as below, in unix using sed or some cammand how to get output as below. cat 1.txt Normal data 1|AA|NN 4|BB|NNN C|DDD|N //Additional details 9C|12C|8N Specific Details 12N|JIN|KK\NY1\ 345\ABC1\KKK 90|ENO0|NDO 17|NO921|NCOKL output shoud be as below ... (10 Replies)
Discussion started by: Tecnical_help12
10 Replies

5. Shell Programming and Scripting

Joining lines in a file - help!

I'm looking for a way to join lines in a file; e.,g consider the following R|This is line 1 R|This is line 2 R|This is line 3 R|This is line 4 R|This is line 5 what i want to end up with is R|This is line 1 R|This is line 2 R|This is line 3 R|This is line 4 R|This is line 5 so... (15 Replies)
Discussion started by: Storms
15 Replies

6. UNIX for Dummies Questions & Answers

JOINING MULTIPLE LINES IN A TEXT FILE USING GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. I WANT THE DATA... (0 Replies)
Discussion started by: KANNI786
0 Replies

7. UNIX for Dummies Questions & Answers

Joining lines of a text file using GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. ... (0 Replies)
Discussion started by: KANNI786
0 Replies

8. Shell Programming and Scripting

awk joining lines

Hello, I'm trying to write a piece of code in awk, which should be able recognize by some regexps two lines and then join them together (maybe write them without \n would be enough, I don't know).. the thing is that every line in the file i'm working with starts with some number, for example: ... (4 Replies)
Discussion started by: midin
4 Replies

9. Shell Programming and Scripting

SED: joining specific lines

I have a .txt file containing lines like: ZAN ZAN name = AI_EVENT desc (further text) ZAN ZAN name = AI_EVENT desc ... ZAN ZAN1 name = AI_EVENT desc ... ZAN ZAN1 name = AI_EVENT desc ... WUR WUR name = AI_EVENT desc ... WUR WUR name = AI_EVENT desc ... VEN VEN name = AI_EVENT desc ... VEN... (2 Replies)
Discussion started by: Gnollish
2 Replies

10. Shell Programming and Scripting

Joining 2 lines in a file together

Hi guys, I've got a log file which has entries that look like this: ------------------------------------------------------------------------------- 06/08/04 07:57:57 AMQ9002: Channel program started. EXPLANATION: Channel program 'INSCCPQ1.HSMTSPQ1' started. ACTION: None. ... (3 Replies)
Discussion started by: m223464
3 Replies
Login or Register to Ask a Question