sed command to arrange words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to arrange words
# 1  
Old 09-30-2015
sed command to arrange words

Hi

I have a text file as given below:

Input format

Code:
I
have
a
text
file
as
given
below
.

This
is
a
sample
test
file
.

There is a space between the sentences in the above format.

I want to write sed command to arrange these words in such a way that they appear as given below.


Output Format:

Code:
I have a text file as given below .
This is a sample test file .

Thanks in advance. Smilie

Last edited by my_Perl; 09-30-2015 at 05:32 PM.. Reason: editing
# 2  
Old 09-30-2015
Code:
sed -e ':L' -e '/\./{p;d;}' -e 'N;s/\n/ /;bL' file

label L
If there is . then print, delete and next cycle.
Append Next line (with embedded \n), substitute the \n by a space, jump to label L
With some debug code you see how it works:
Code:
sed -e ':L' -e '/\./{p;d;}' -e '=;p;N;s/\n/ /;bL' file

print current line number and current pattern space.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 09-30-2015
I got the following output:

Code:
I
have
a
text
file
as
given
below
.

This
is
a
sample
test
file
.
I have a text file as given below .
 This is a sample test file .


But I want to get the following output only:

Code:
I have a text file as given below .
This is a sample test file .

# 4  
Old 09-30-2015
Code:
sed -e ':L' -e '/\./{p;d;}' -e 's/^ //' -e 'N;s/\n/ /;bL'

Will do the trick!

---------- Post updated at 04:46 PM ---------- Previous update was at 04:44 PM ----------

Sorry, new here--

Code:
sed -e ':L' -e '/\./{p;d;}' -e 's/^ //' -e 'N;s/\n/ /;bL'

let's see if this is kosher!

Last edited by Scrutinizer; 10-03-2015 at 04:27 AM.. Reason: Corrected code tags
This User Gave Thanks to Klasform For This Post:
# 5  
Old 09-30-2015
I got the following output:

Code:
I
have
a
text
file
as
given
below
.

This
is
a
sample
test
file
.
I have a text file as given below .
This is a sample test file .

How do I get only:

Code:
I have a text file as given below .
This is a sample test file .


Thanks.Smilie
# 6  
Old 09-30-2015
Code:
sed -e ':L' -e '/\./{p;d;}' -e 's/^ //' -e 'N;s/\n/ /;bL'

Ah git it!!
This User Gave Thanks to Klasform For This Post:
# 7  
Old 09-30-2015
Or substitute the leading space only once before the print
Code:
sed -e ':L' -e '/\./{s/^ //;p;d;}' -e 'N;s/\n/ /;bL' file

You don't get the input repeated in the output. (What did you do?)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Arrange output of a command

Hi, I am using a command :- ps -auxwww 2> /dev/null | awk 'match ($0, GIN:*/) {print substr($0, RSTART+3, RLENGTH-3)}' | sort | grep -v -F 'which gives output as 3 aruau 5 asun 4 cgan Now I need a command which gives me something like this :- 3 aruau 5 asun 4 cgan... (2 Replies)
Discussion started by: Raj999
2 Replies

2. Shell Programming and Scripting

Reverse words with sed

Hi guys, I'm wondering what the best way would be to reverse words in a string based on certain characters. For example: echo Word1Word2Word3Word4 | sed ' /\n/ !G s/\(Word.\)\(.*\n\)/&\2\1/ //D ' This returns: Word4Word3Word2Word1 I'm no sed expert but... (2 Replies)
Discussion started by: Subbeh
2 Replies

3. Shell Programming and Scripting

Swap words using sed

Hi. I have to swap the first and the third word in all lines of a txt file using sed. Separators between words are: any charachter, except intervall. I hope, you'll understand what I want to do. my english is not so good, sorry for that:) (10 Replies)
Discussion started by: T720
10 Replies

4. Shell Programming and Scripting

How to words in arrange as Table

Hi Team, I have one file in that almost 100+ words in lines Eg:- Unix windows solaris Linux ... ... But I want arrange all lines in table format and can able read on screen Eg: - Unix windows solaris Lunix...... Hp unix Mac-os ...... Like as Table...... (11 Replies)
Discussion started by: Bhaskar Alagala
11 Replies

5. Shell Programming and Scripting

Find and arrange words with same letters from list

I am making a word game and I am wondering how to find and arrange words in a list that have the same letters. In my game, you are presented with 5 letters, and you then have to rearrange the letters tp make a word. So the word could be "acorn", but those 5 letters could also make up "narco" or... (2 Replies)
Discussion started by: hellobard
2 Replies

6. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

7. Shell Programming and Scripting

arrange merged data using sed

hi, i used paste file1.txt file2.txt > file3.txt to merge 2 columns from file1 and 4 columns from file2. file1 scaffold_217 scaffold_217 file2 CHRSM N scaffold_217.pf scaffold_217.fsa the result is as follows:- scaffold_217 scaffold_217 CHRSM ... (6 Replies)
Discussion started by: redse171
6 Replies

8. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

9. Shell Programming and Scripting

search two words in sed

I've following sed command working fine - sed '/search_pattern1/ !s/pattern1/pattern2/" file Now, I want to search two patterns - search_pattern1 and search_pattern2 . How can put these into above sed statement ? Thanks in advance. (12 Replies)
Discussion started by: ajitkumar2
12 Replies

10. UNIX for Dummies Questions & Answers

sed [delete everything between two words]

Hi, I have the following codes below that aims to delete every words between two pattern word. Say I have the files To delete every word between WISH_LIST=" and " I used the below codes (but its not working): #!/bin/sh sed ' /WISH_LIST=\"/ { N /\n.*\"/ {... (3 Replies)
Discussion started by: Orbix
3 Replies
Login or Register to Ask a Question