SED or AWK "append line to the previous line"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED or AWK "append line to the previous line"
# 22  
Old 05-17-2009
The AWK code working file
but you are right the SED code is not 100% completely

cat file.text

1234;test;test;test;
;test;test;test;test
beta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
test
;test;test;test;test
charlie;text
test
;test;test;test;test
dog;text
1234567789;test;alpha;beta


Code:
awk 'NR==1{s=$0;next} /^[A-Z]|^;/{s=s$0;next} {print s;s=$0} END{if(s)print s}' file.text

1234;test;test;test;;test;test;test;testbeta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;test;test;test;test;testcharlie;texttest;test;test;test;testdog;text
1234567789;test;alpha;beta


Code:
awk 'BEGIN{s=""}/^[0-9]/ && NR==1 || !/^[0-9]/{ s=s $0}/^[0-9]/ && NR>1{ s=s "\n" $0}END{print  s}' file.text

1234;test;test;test;;test;test;test;testbeta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;test;test;test;test;testcharlie;texttest;test;test;test;testdog;text
1234567789;test;alpha;beta


Code:
sed -n '/^[a-z]/!{$!{N;/.*\n;.*/{s/\(.*\)\n\(;.*\)*/\1\2/g;p;d};/.*\n;.*/!{P;D}};p}' file.text

1234;test;test;test;;test;test;test;test
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
;test;test;test;test
;test;test;test;test
1234567789;test;alpha;beta
# 23  
Old 05-17-2009
Quote:
Originally Posted by ghostdog74
you sure its correct?
Are you sure you get a wrong output, this is what I get:
Code:
$ cat file
1234;test;test;test;
;test;test;test;test
beta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
test
;test;test;test;test
charlie;text
test
;test;test;test;test
dog;text
1234567789;test;alpha;beta
$
$ awk 'NR==1{s=$0;next} /^[A-Z]|^;/{s=s$0;next} {print s;s=$0} END{if(s)print s}' file
1234;test;test;test;;test;test;test;testbeta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;test;test;test;test;testcharlie;texttest;test;test;test;testdog;text
1234567789;test;alpha;beta
$

# 24  
Old 05-17-2009
That's my output:

Code:
awk --version GNU Awk 3.1.5
sed --version GNU sed version 4.1.5

$ cat file.txt
1234;test;test;test;
;test;test;test;test
beta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
test
;test;test;test;test
charlie;text
test
;test;test;test;test
dog;text
1234567789;test;alpha;beta

$ awk 'BEGIN{s=""}/^[0-9]/ && NR==1 || !/^[0-9]/{ s=s $0}/^[0-9]/ && NR>1{ s=s "\n" $0}END{print  s}' file.txt

1234;test;test;test;;test;test;test;testbeta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;test;test;test;test;testcharlie;texttest;test;test;test;testdog;text
1234567789;test;alpha;beta
 

$ awk 'NR==1{s=$0;next} /^[A-Z]|^;/{s=s$0;next} {print s;s=$0} END{if(s)print s}' file.txt

1234;test;test;test;;test;test;test;testbeta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;test;test;test;test;testcharlie;texttest;test;test;test;testdog;text
1234567789;test;alpha;beta

$!sed 
sed -n '/^[a-z]/!{$!{N;/.*\n;.*/{s/\(.*\)\n\(;.*\)*/\1\2/g;p;d};/.*\n;.*/!{P;D}};p}' file.txt

1234;test;test;test;;test;test;test;test
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
;test;test;test;test
;test;test;test;test
1234567789;test;alpha;beta

Is something different like in my previous post?
# 25  
Old 05-17-2009
Quote:
Originally Posted by Franklin52
Are you sure you get a wrong output, this is what I get:
yes
Code:
# more file
1234;test;test;test;
;test;test;test;test
beta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
test
;test;test;test;test
charlie;text
test
;test;test;test;test
dog;text
1234567789;test;alpha;beta

# awk 'NR==1{s=$0;next} /^[A-Z]|^;/{s=s$0;next} {print s;s=$0} END{if(s)print s}' file
1234;test;test;test;;test;test;test;test
beta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;
test;test;test;test;test
charlie;text
test;test;test;test;test
dog;text
1234567789;test;alpha;beta

changing [A-Z] to [a-zA-Z] would solve it.
# 26  
Old 05-17-2009
Code:
sed -n '1 {
h
}
1 !{
	/^;/{
		H
	}
	/^;/!{
		x
		s/\n//g
		p
	}
}' a.txt


perl:
Code:
undef $/;
open $fh,"<","a.txt";
my $str=<$fh>;
$str=~s/\n;/;/g;
print $str;


Last edited by summer_cherry; 05-17-2009 at 10:47 PM..
# 27  
Old 05-18-2009
-ghostdog74-
I'm 100% sure about my output!
Is that a bug in my awk version?
# 28  
Old 05-18-2009
Quote:
Originally Posted by research3
-ghostdog74-
I'm 100% sure about my output!
Is that a bug in my awk version?
here's is something to think about, this pattern from your code
Code:
 /^[A-Z]|^;/

means search for upper case letter at start of line OR semi-colon at start of line. your sample data doesn't contain upper case at start of line. I have also mentioned to include the lower case to resolve it
Code:
 # awk 'NR==1{s=$0;next} /^[a-zA-Z]|^;/{s=s$0;next} {print s;s=$0} END{if(s)print s}' file
1234;test;test;test;;test;test;test;testbeta;text
01234;test;alpha;beta
47888;test;test;test;test
88899;test;test;test;test;test;test;test;testcharlie;texttest;test;test;test;testdog;text
1234567789;test;alpha;beta

( Unless your awk is already set to recognize case-insensittive somewhere ??? )
however, its still not very flexible,, as your data may contain other characters besides a-zA-Z and ; at the start of line, so using !/^[0-9]/ may be more "flexible".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Append a line "while read line"

Hello everyone, I want to process a data file using bash, I would like add a new line RIGHT after the time when i know "another variable met a value" Like this: while read line do ... if ; then #Append a new line "===" to input.txt file after $line position #break fi done <... (11 Replies)
Discussion started by: fongthai
11 Replies

3. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

4. Shell Programming and Scripting

Is it possible to use sed to handle the line contains BOTH "AA" and "BB"

if yes, can some expert give an example Lei (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. Shell Programming and Scripting

AWK for multiple line records RS="^" FS="#"

I have to pull multiple line records with ^ as the record separator(RS)... # should be my field separator (FS)... Sample record is: ^-60#ORA-00060: deadlock detected while waiting for resource ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012... (7 Replies)
Discussion started by: Vidhyaprakash
7 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

append "awk command" to the end of each line

hi; this is my qqq.mos: l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-' l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-' l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-' ... i want to append;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r"... (4 Replies)
Discussion started by: gc_sw
4 Replies

8. Shell Programming and Scripting

sed append "\n" to end of every line in file

I know it sounds simple, but I want to e-mail the last 6 lines of a log file, which I have tailed into logresults.txt. I'm using echo -e "Subject:server results\nFrom:server log <user@domain.com>\n"`cat logresults.txt` | sendmail -t user@domain.com which works, but the body of the e-mail has... (4 Replies)
Discussion started by: unclecameron
4 Replies

9. Shell Programming and Scripting

each line as 20digit long append zero "0" in front

i have an ip file like 121 1213412 34345353 long file want to made each line as 20digit long append zero "0" in front as 121 become 00000000000000000121 (1 Reply)
Discussion started by: RahulJoshi
1 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question