Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manipulate \n

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hello,

Another silly question probably, but here goes.

Suppose I have a text file that looks like this:

;
42
;
33
;
437
;
16029
;
33

;
73
;
20
;
267
;
11029
;
45

and I want it to look like this:

;42;33;437;16029;33
;73;20;167;11029;45

how would I do that?

If I do cat file | tr -d '\n' it gives it all on one line.
There is a \n after every ; and number, with an additional \n between the sets. Any ideas?
 
..additionally...

i was hoping the tr -s command would do it, since thare are places where there are 2 \n's , but I can't get the syntax working.
 

nawk -f a.awk a.txt

#-------------------- a.awk
$0 ~ "^$" {print;next}
{ printf("%s", $0); }
END { print}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
huh? will give it a go....
 
try:

#!/usr/bin/ksh
INFILE=$1
STRING=&quot;&quot;

cat $INFILE | while read LINE; do
echo $LINE | grep &quot;^$&quot;
if [ $? -eq 0 ]; then
STRING=&quot;${STRING}\n&quot;
else
STRING=&quot;${STRING}${LINE}&quot;
fi
done

echo $STRING


EDC
 
vlad's stuff does it perfectly. created a file called a.awk as specified and ran it - voila ! I'll be damned if I have any idea how though.
Guru vlad - thank you.

ecasadella: i'll try your version as well...
 
hi

or you can try this

cat file1 | while read aaa
do
print &quot;$aaa\c&quot;
if [[ $aaa = &quot;&quot; ]]
then
print &quot; \n&quot;
fi
done
 
vlad...?
would it be possible for you to quickly explain the above nawk programme?
 
sure - here it goes:

# nawk [NewAwk] invokation line
nawk -f a.awk a.txt

#-------------------- a.awk
# for any 'blank' line (a line [$0] that has ONLY a begining (^)
# and an end ($) with nothing else] print out a NEWLINE (print)
# and proceed to the next (next) record/line
$0 ~ &quot;^$&quot; {print;next}
#
# for any OTHER line [non-empty line] - print out the line
# with NO CR/LF (printf)
{ printf(&quot;%s&quot;, $0); }
#
# don't forget the LAST CR/LF for the LAST record/line
END { print}

#--------------------------------
I've posted a somewhat better/shorter/succinct solution in the other thread - that solution takes advantage of awk's RS [RecordSeparator] being an &quot;empty line&quot;.
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+ vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Here's a one-liner ( use backslash bang '\!' in some shells )

sed -e :a -e 'N' -e '$!{' -e '/\n$/!ba' -e '}' -e 's/\n//g' a.txt

Or as

sed '
:a
N
$!{
/\n$/!ba
}
s/\n//g
' a.txt Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
So many methods. Here's my 2 cents:
Code:
perl -p -e 's/(.)\n/$1/;' test.txt > new.txt
Or an in place modification:
Code:
perl -i -p -e 's/(.)\n/$1/;' test.txt
Cheers, Neil :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top