LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   use sed to swap text in a string (not replace) (https://www.linuxquestions.org/questions/linux-newbie-8/use-sed-to-swap-text-in-a-string-not-replace-4175690660/)

Gebby_3 02-17-2021 08:37 AM

use sed to swap text in a string (not replace)
 
I would like to swap text by using " - " as the swap position and send output of the swapped line.
There are multiple lines to search thru and swap.
There is only 1 occurrence of " - " in each line and of course different positions according to the length of each.

ex Input line:
Group Name - Song Title

ex Output line:
Song Title - Group Name

sed (condition statements) input file > output file

I am using cgywin, windows 10, mks toolkit and tcc by j.p.software

rknichols 02-17-2021 08:55 AM

Quote:

Originally Posted by Gebby_3 (Post 6221303)
I would like to swap text by using " - " as the swap position and send output of the swapped line.
There are multiple lines to search thru and swap.
There is only 1 occurrence of " - " in each line and of course different positions according to the length of each.

From the sed info page (in the section about the 's' command):
"The REPLACEMENT can contain `\N' (N being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the Nth `\(' and its matching `\)'."
So, piece of cake:
Code:

sed 's/\(.*\) - \(.*\)/\2 - \1/' input_file >output_file

shruggy 02-17-2021 09:49 AM

Must it be sed?
Code:

echo Group Name - Song Title|awk -F' - ' '$0=$2FS$1'

Gebby_3 02-17-2021 10:01 AM

Use of awk instead of sed????
 
Quote:

Originally Posted by shruggy (Post 6221323)
Must it be sed?
Code:

echo Group Name - Song Title|awk -F' - ' '$0=$2FS$1'

Sorry, but I'm not familiar with awk. How would I interpret input to output?

shruggy 02-17-2021 10:03 AM

Same as in sed
Code:

awk -F' - ' '$0=$2FS$1' input_file >output_file

Gebby_3 02-17-2021 04:34 PM

awk vs sed
 
Quote:

Originally Posted by shruggy (Post 6221329)
Same as in sed
Code:

awk -F' - ' '$0=$2FS$1' input_file >output_file


Thanks. I've only started learning scripting for a couple of days. Gives me the chance to study and learn.

I'm working on my next and hopefully last lesson for a bit.

Getting ready to build my scripting files in a tcc batch file.

Having a snag with a comma when pulling out %groupname ex: Beck, Jeff - Song title

I'll be posting my problem shortly. Have to gather my thoughts to put it out there properly.

sed -n '/%groupname/p' !bygroup.lst > %filename.lst

shruggy 02-17-2021 04:44 PM

TCC is a Windows shell and not a standard one as well. I'm afraid nobody here can advise you on any quoting issues in TCC. You have to peruse the TCC docs. Comma wouldn't be an issue inside a single-quoted string in any Linux shell.

Try enclosing the sed expression in double quotes and see if it'll work better
Code:

sed -n "/%groupname/p" !bygroup.lst > %filename.lst

Gebby_3 02-17-2021 06:50 PM

sed double quotes to include comma in expression
 
Quote:

Originally Posted by shruggy (Post 6221457)
TCC is a Windows shell and not a standard one as well. I'm afraid nobody here can advise you on any quoting issues in TCC. You have to peruse the TCC docs. Comma wouldn't be an issue inside a single-quoted string in any Linux shell.

Try enclosing the sed expression in double quotes and see if it'll work better
Code:

sed -n "/%groupname/p" !bygroup.lst > %filename.lst

Shruggy,
Worked like a charm!
--Thanks

shruggy 02-18-2021 02:23 AM

Since you're using Cygwin anyway, it would make sense to install bash from there. This way you'll get a much more Unix-like environment. TCC with GNU tools is a very unusual combination so you're mostly on your own when trying this. Most guides, howtos and textbooks that teach you how to use GNU tools, assume you're using bash or similar shell. Besides, you'll be able to check the syntax with https://www.shellcheck.net which alone can justify the switch.

Gebby_3 03-08-2021 06:45 AM

Reverse right side to left at " - "
 
Quote:

Originally Posted by rknichols (Post 6221311)
From the sed info page (in the section about the 's' command):
"The REPLACEMENT can contain `\N' (N being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the Nth `\(' and its matching `\)'."
So, piece of cake:
Code:

sed 's/\(.*\) - \(.*\)/\2 - \1/' input_file >output_file

sed 's/\(.*\) - \(.*\)/\2 - \1/' input_file > output_file
sed "s/\(.*\) - \(.*\)/\2 - \1/" input_file > output_file

I need help trying to understand and fix this syntax.
It works fine depending on which flavor of sed I use, enclosed with ' ' or " "
The result prints the reversed output in 2 separate lines not 1.

pan64 03-08-2021 07:31 AM

what part is unclear? What should be fixed (and why)?

Gebby_3 03-08-2021 07:43 AM

Quote:

Originally Posted by pan64 (Post 6228407)
what part is unclear? What should be fixed (and why)?

My output needs to be on a single line not in separate lines

{left - right} to {right - left}
not
right
- left

another ex:
swap {first name - last name} to {last name - first name} using " - " as line separator

pan64 03-08-2021 07:54 AM

the EOL (end of line) is different on windows and linux, and sed works on linux, so that EOL probably not handled properly. Try:
Code:

sed 's/\(.*\)\r? - \(.*\)/\2 - \1/' input_file >output_file

boughtonp 03-08-2021 08:02 AM

Quote:

Originally Posted by Gebby_3 (Post 6228391)
It works fine depending on which flavor of sed I use, enclosed with ' ' or " "

See https://www.gnu.org/software/bash/manual/html_node/Quoting.html

Quote:

Originally Posted by Gebby_3 (Post 6228391)
The result prints the reversed output in 2 separate lines not 1.

Post the results of:
Code:

sh --version
sed --version
cat -A input_file
cat -A output_file

(The latter two don't need to be the entire output, just the parts corresponding to the relevant line(s).)


Gebby_3 03-08-2021 08:14 AM

Quote:

Originally Posted by pan64 (Post 6228413)
the EOL (end of line) is different on windows and linux, and sed works on linux, so that EOL probably not handled properly. Try:
Code:

sed 's/\(.*\)\r? - \(.*\)/\2 - \1/' input_file >output_file

That syntax left the line unchanged.

This works but I am trying to simulate with sed (GNU sed version 3.02)
(Gawk GNU Awk 3.1.0)
gawk -F" - " "$0=$2FS$1" input file > output file


All times are GMT -5. The time now is 11:35 AM.