Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to replaces patterns and cleanup a file containing multiple words to get one word per line.

The result is achieved using this command line:

sed -e '/^[[:space:]]*$/ d' \             # remove empty line
    -e 's/^[[:space:]]*//' \              # remove white space at the beginning
    -e 's/[[:space:]]*$//' \              # remove white space at the ending (EOL)
    -e 's/[[:space:]][[:space:]]*/\n/g' \ # convert blanks between words to newline
    -e '$a\'                              # add a newline if missing at EOF
    -e .....                              # replace other patterns.

(the last expression was found in How to add a newline to the end of a file?)

The idea is to process the file (eg. replaces some pattern) and format the file at the same time with only one small sed program.

I'm sure its possible to use other sed features to reduces the expression.

Regards

share|improve this question

4 Answers 4

Try this

sed -e 's/[[:space:]]/\n/g' | grep -v '^$'

It uses both grep and sed, but I hope it's OK (if you have sed on a system, you usually have grep too)

share|improve this answer
    
@evilsoup, no, that wouldn't work. /^$/d matches on an empty pattern space, the pattern space would not become empty as a result of the first s command. –  Stéphane Chazelas Jun 14 '13 at 17:11
    
@Karel - (GNU) sed only: sed 's/ \+/\n/g' infile | sed '/^$/d;$a\' > outfile –  don_crissti Jun 15 '13 at 9:16
    
@Karel-Bilek: while it's working, eg. it's putting words one per line, removing all spaces, add a new line at EOF: could this be done with a single invocation of 'sed' without any other Unix tool ? Regards. –  ydroneaud Jun 17 '13 at 8:54
    
@ydroneaud: don_crissti wrote exactly that. But it needs GNU version of sed. Not sure about UNIX standard. –  Karel Bílek Jun 17 '13 at 21:11
    
@KarelBílek not exactly as it's using 2 invocations of sed with a pipe. I would like only one sed invocation. –  ydroneaud Jun 18 '13 at 13:31

You can use tr:

tr -s "[[:blank:]]" "\n" < file | grep .

The [:blank:] character class includes all horizontal whitespace. The -s squeezes or reduces multiple character occurrences to one.

The grep removes a blank line (if present).

share|improve this answer
    
Thanks for the suggestion, but it's not using sed and an empty line in the beginning of the input file is not removed and a newline is not added after the last word. Regards. –  ydroneaud Jun 17 '13 at 8:49

With perl:

perl -ane 'foreach (@F) {print "$_\n";}' infile > outfile

Without sed:

for word in $(cat infile); do echo "$word" >> outfile; done

share|improve this answer
1  
However, be aware that if infile contains glob characters (*, ? or certain cases of [...]), they might get expanded. Also, if infile is very big, you'll use a lot of memory expanding $(cat infile). –  rici Jun 14 '13 at 16:48
1  
This might be more secure: while read -ra words; do (( ${#words[@]} )) && printf "%s\n" "${words[@]}"; done < file –  glenn jackman Jun 14 '13 at 17:08
    
Perl is not an option in my case, regards. –  ydroneaud Jun 17 '13 at 8:54
    
@ydroneaud - this could be done with a single sed invocation but ONLY if the file is missing the newline at EOF. If the file already has a new line at EOF it would add a second new line which is probably not what you want. –  don_crissti Jun 19 '13 at 18:36

Not sed, but:

gawk length RS='[[:space:]]+' file

That treats any sequence of whitespace as the record separator, and prints each non-empty record.

share|improve this answer
    
It's GNU-specific though. Other awk only support single-character as the record separator. –  Stéphane Chazelas Jun 14 '13 at 17:24
    
@don_crissti, leading spaces are actually removed, but awk treats the empty string before the whitespace as an empty record. I've updated to only print non-empty records –  glenn jackman Jun 14 '13 at 18:45
    
To convince me to use awk instead of sed, you need to show how to replace multiple patterns in the file while formatting it with a POSIX awk. Regards. –  ydroneaud Jun 17 '13 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.