djanu
October 15, 2008, 3:14pm
1
Hi,
I am trying to use awk to split line before the pattern:
abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890
I need it to be like this:
abcde 12345 67890
abcde 12345 67890
abcde 12345 67890
abcde 12345 67890
abcde 12345 67890
What the easiest way to do it?
Thanks,
DJ
sed 's/abcde/~abcde/g' < inputfilename | tr -s '~' '\n' > newfile
one way.
djanu
October 15, 2008, 5:32pm
3
This is what I needed. Thanks!
cat filename | tr 'abcde' '\nabcde'
tr is not replacing the string, the above command will change all the occurance of
'a' with '\n'
'b' with 'a'
'c' with 'd'
'e' with ''
cat filename | tr 'abcde' '\nabcde'
the above code works fine here..
Or:
(use /usr/xpg4/bin/sed on Solaris)
sed 's/\([^ ]* \)\{3\}/&\
/g' infile
If your sed implementation does not support re-interval:
sed 's/\([^ ]* [^ ]* [^ ]* \)/&\
/g' infile
or:
printf "%s %s %s\n" $(<infile)
Check you result characted 'e' will be missing. put some 'a' in your file and try that command.
$ cat filename
abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12
345 67890 abcde 12345 67890 abcde 12345 67890
l675389@ap1dev3 : $ cat filename | tr 'abcde' '\nabcde'
abcd 12345 67890
abcd 12345 67890
abcd 12345 67890
abcd 12345 67890
abcd 12345 67890
abcd 12345 67890
abcd 12345 67890
$ man tr
tr(1) tr(1)
NAME
tr - translate characters
SYNOPSIS
tr [-Acs] string1 string2
tr -s [-Ac] string1
tr -d [-Ac] string1
tr -ds [-Ac] string1 string1
DESCRIPTION
tr copies the standard input to the standard output with substitution
or deletion of selected characters. Input characters from string1 are
replaced with the corresponding characters in string2. If necessary,
string1 and string2 can be quoted to avoid pattern matching by the
shell.