Reputation: 41
I want to append every line in my file that begins with spaces to the previous line.
INPUT FILE
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc
=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad
,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=
rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad
,dc=lan
OUTPUT FILE
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-epot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
Thanks in advance
Upvotes: 1
Views: 811
Reputation: 203712
Given the data you posted:
$ awk '{ORS=(sub(/^ /,"")?RS:"")}1' file
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
With other data YMMV so hopefully you put some thought into your sample.
Upvotes: 0
Reputation: 58453
This might work for you (GNU sed):
sed ':a;N;s/\n //;ta;P;D' file
Read two lines into the pattern space and if an appended line begins with a space, remove the preceeding newline and the space and repeat. Otherwise, print the first line in the pattern space, delete the first line and repeat.
Upvotes: 0
Reputation: 3089
Possible solutions using awk
$ awk -v RS= '{gsub(/\n /,"")}1' file
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
$ awk -v OFS= -v ORS= '/^ /{$NF=$NF RS}1' file
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
$ awk -v ORS= '/^ /{ sub(/ /,""); $NF=$NF RS}1' file
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
$ awk '/^[^ ]/{ORS=""} /^ /{OFS=""; $1=$1; ORS=RS}1' file
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
I would recommend 1st, 2nd and 3rd soluion.
4th one is just more verbose version of 2nd one.
There are more but I think this would be sufficient.
Upvotes: 1
Reputation: 158060
With sed
:
sed ':a;N;$!ba;s/\n //g' file
Better explained in multiline version:
# Define a label called 'a'
:a
# Read the next line of input and append it to the
# internal pattern buffer
N
# $ means 'the last line of input'
# ! negates the match
# ba (b)ranches back to label 'a'
# Means:
# Unless we've reached the end of the input file
# read the file line by line into the pattern buffer
$!ba
# Once the end of the input file is reached we substitute
# every occurrence of newline+space by an empty string
s/\n //g
# sed will print the result of the substitution by default
PS: Depending on your version of sed
you may need to use:
s/
//g
instead of
s/\n //g
Meaning you need to use a literal newline if \n
isn't supported
And btw, if your input file only contains pairs of two lines where one line has to be appended to the previous, and only that(!), then it could be as as simple as:
sed 'N;s/\n //' file
This would also not require to buffer the whole file in memory.
Upvotes: 1
Reputation: 67497
awk
to the rescue!
$ awk '{if(sub(/^ /,p,$0)) print; else p=$0}' file
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
Upvotes: 0
Reputation: 92854
Another awk approach:
awk '{ printf "%s%s",$1,(/^ /? ORS:"") }' file
The output:
roleOccupant:uid=standard_rhc,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_test,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_NLHaNA,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
roleOccupant:uid=standard_dept,ou=tenants,ou=users,dc=e-depot,dc=rad,dc=lan
Upvotes: 0
Reputation: 133545
Following awk may help you in same.
awk '{sub(/^ +/,"");printf("%s%s",$0~/^role/?(NR==1?$0:RS $0):$0,$0)} END{print ""}' Input_file
Upvotes: 0