What shell script should I use in Linux to replace a group with n lines of text with a single line?
I have a file like :
a
b
c
*
d
e
f
*
g
h
i
*
and I want to get a file as:
abc
def
ghi
can use awk
awk '{if ($0=="*"){print s;s=""}else{s=s$0}}' file
an bash way to this is
while read x
do
[ "$x" == "*" ] && echo || echo -n $x
done < file
sed way:
sed ':a;N;$!ba;s/\n//g' < t | sed 's:*:\n:g'
t is the file you want to change.
references: How can I replace a newline (\n) using sed? Why does sed not replace all occurrences?
The first command replaces \n with nothing. the second replaces * with \n.
sed is a very powerful stream editor tool by the way, knowing it can help you in more ways than you can imagine.
Other than awk solution :
tr '\n' ' ' < Input.txt |sed 's/ //g' | tr '*' '\n'