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 |
Quote:
"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 |
Must it be sed?
Code:
echo Group Name - Song Title|awk -F' - ' '$0=$2FS$1' |
Use of awk instead of sed????
Quote:
|
Same as in sed
Code:
awk -F' - ' '$0=$2FS$1' input_file >output_file |
awk vs sed
Quote:
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 |
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 |
sed double quotes to include comma in expression
Quote:
Worked like a charm! --Thanks |
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.
|
Reverse right side to left at " - "
Quote:
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. |
what part is unclear? What should be fixed (and why)?
|
Quote:
{left - right} to {right - left} not right - left another ex: swap {first name - last name} to {last name - first name} using " - " as line separator |
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 |
Quote:
Quote:
Code:
sh --version |
Quote:
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. |