Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Improved punctuation; fixed typo; added tag.
Source Link

I have a text file (1.txt) as follows:

 word1  word2  word3 

I use sed to add strings to the beginning and end of every line as follows:

 sed 's/^/blahblah1 /g' 1.txt > 2.txt  sed 's/$/ blahblah2/g' 2.txt > 3.txt 

resulting in:

 blahblah1 word1 blahblah2  blahblah1 word2 blahblah2  blahblah1 word3 blahblah2 

This works fine, but I want to do something further.

I want to copy the strings in the middle of each line (word1, word2, etc.) to the end of every line as follows:

 blahblah1 word1 blahblah2 word1  blahblah1 word2 blahblah2 word2  blahblah1 word3 blahblah2 word3 

PLEASE NOTE that blahblah1 and blahblah2 are just representing bash commands and other string text AND are the same at the beginning and end of every line as the sed commands illustrate - the strings between them are DIFFERENT - andthe– and the strings are not actually "blahblah1" and blahblah2""blahblah2".

Any suggestions?

Basically, I'm taking a text file containing a list of strings and want to create a bash script containing a list of commands to grab lines of code containing a string and dump them to files with a file name containing that string - hence the structure of each line.

I have a text file (1.txt) as follows:

 word1  word2  word3 

I use sed to add strings to the beginning and end of every line as follows:

 sed 's/^/blahblah1 /g' 1.txt > 2.txt  sed 's/$/ blahblah2/g' 2.txt > 3.txt 

resulting in:

 blahblah1 word1 blahblah2  blahblah1 word2 blahblah2  blahblah1 word3 blahblah2 

This works fine but I want to do something further.

I want to copy the strings in the middle of each line (word1, word2 etc) to the end of every line as follows:

 blahblah1 word1 blahblah2 word1  blahblah1 word2 blahblah2 word2  blahblah1 word3 blahblah2 word3 

PLEASE NOTE that blahblah1 and blahblah2 are just representing bash commands and other string text AND are the same at the beginning and end of every line as the sed commands illustrate - the strings between them are DIFFERENT - andthe strings are not actually "blahblah1" and blahblah2".

Any suggestions?

Basically, I'm taking a text file containing a list of strings and want to create a bash script containing a list of commands to grab lines of code containing a string and dump them to files with a file name containing that string - hence the structure of each line.

I have a text file (1.txt) as follows:

word1 word2 word3 

I use sed to add strings to the beginning and end of every line as follows:

sed 's/^/blahblah1 /g' 1.txt > 2.txt sed 's/$/ blahblah2/g' 2.txt > 3.txt 

resulting in:

blahblah1 word1 blahblah2 blahblah1 word2 blahblah2 blahblah1 word3 blahblah2 

This works fine, but I want to do something further.

I want to copy the strings in the middle of each line (word1, word2, etc.) to the end of every line as follows:

blahblah1 word1 blahblah2 word1 blahblah1 word2 blahblah2 word2 blahblah1 word3 blahblah2 word3 

PLEASE NOTE that blahblah1 and blahblah2 are just representing bash commands and other string text AND are the same at the beginning and end of every line as the sed commands illustrate the strings between them are DIFFERENT – and the strings are not actually "blahblah1" and "blahblah2".

Any suggestions?

Basically, I'm taking a text file containing a list of strings and want to create a bash script containing a list of commands to grab lines of code containing a string and dump them to files with a file name containing that string hence the structure of each line.

Tweeted twitter.com/StackUnix/status/818445838368260097
Source Link
speld_rwong
  • 839
  • 2
  • 15
  • 23

How to copy strings from middle of lines (between strings) to the end of lines

I have a text file (1.txt) as follows:

 word1 word2 word3 

I use sed to add strings to the beginning and end of every line as follows:

 sed 's/^/blahblah1 /g' 1.txt > 2.txt sed 's/$/ blahblah2/g' 2.txt > 3.txt 

resulting in:

 blahblah1 word1 blahblah2 blahblah1 word2 blahblah2 blahblah1 word3 blahblah2 

This works fine but I want to do something further.

I want to copy the strings in the middle of each line (word1, word2 etc) to the end of every line as follows:

 blahblah1 word1 blahblah2 word1 blahblah1 word2 blahblah2 word2 blahblah1 word3 blahblah2 word3 

PLEASE NOTE that blahblah1 and blahblah2 are just representing bash commands and other string text AND are the same at the beginning and end of every line as the sed commands illustrate - the strings between them are DIFFERENT - andthe strings are not actually "blahblah1" and blahblah2".

Any suggestions?

Basically, I'm taking a text file containing a list of strings and want to create a bash script containing a list of commands to grab lines of code containing a string and dump them to files with a file name containing that string - hence the structure of each line.