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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AIX sed command don't work with \n

Status
Not open for further replies.

ecollard

Technical User
Aug 31, 2005
10
FR
Hi,

i want to use the sed command on AIX (5.x) and i have a problem with special character like \n ou \t

the command like cat file| sed 's/\n//g' don't work.

I have tried : cat file| sed 's/\\n//g'
Etc...

do you have an idea ?
 
Try using Ctrl-V and then the relevant key
For example type 'sed 's/Ctrl-v<return>//g' and you get
Code:
sed 's/^M//g'
Then don't use cat unless you need to
Try
Code:
sed 's/^M//g <input_file > output_file

Ceci n'est pas une signature
Columb Healy
 
Discussing this with a co-worker who points out that what I've described removes \r, not \n. He recommends using tr i.e.
Code:
tr -d '\n' < inputfile > outputfile # remove line feeds
tr -d '\t' < inputfile > outputfile # remove tabs

Ceci n'est pas une signature
Columb Healy
 
OK,
but my sample is not good.

other case inverse , i want to add a new line ou tab after a blank ?

sed 's/ /\n/g <input_file > output_file

Q: why the man AIX speak about and Linux work ?
 
Hmmm...

You're right, and it gets more complex than that. However the exaple in 'man sed' does work which is
man sed said:
Code:
  5. A sample sed script file:

     :join

     /\\$/{N

     s/\\\n//

     b join

     }

     This sed script joins each line that ends with a \ (backslash) to the line
     that follows it. First, the pattern /\\$/ selects a line that ends with a \
     for the group of commands enclosed in {} (braces). The N subcommand then
     appends the next line, embedding a new-line character. The s/\\\n// deletes
     the \ and embedded new-line character. Finally, b join branches back to the
     label :join to check for a \ at the end of the newly joined line. Without
     the branch, the sed command writes the joined line and reads the next one
     before checking for a second \.

         Note: The N subcommand causes the sed command to stop immediately if
         there are no more lines of input (that is, if the N subcommand reads an
         end-of-file character). It does not copy the pattern space to standard
         output before stopping. This means that if the last line of the input
         ends with a \, it is not copied to the output.
which shows that you can use the \n format.

However, I can't get it to work pn the right hand side of the substitution. We need a sed guru

Ceci n'est pas une signature
Columb Healy
 
Cracked it
Your 'add an LF after every space' script would be
Code:
{N
s/ / \
/g
}
On the command line - cut and pasted from my screen
Code:
b01301# cat hello.txt
Hello, world
b01301# sed 's/ / \
> /g' < hello.txt
Hello,
world
i.e. use the return key after a backslash.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top