Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, April 30, 2016

if a string exist (including a variable), flip it using awk or sed

if a string exist (including a variable), flip it using awk or sed


I have a csv file like this:

,College Level Math 55,Elementary Algebra 112  ,Elementary Algebra 79,  ,College Level Math 102,Elementary Algebra 54  ,,College Level Math 54  

I need an awk or sed command that does the following

if College Level Math *,Elementary Alegrbra * exist flip it so it looks like

Elementary Algebra *, College Level Math *  

Desired output

,Elementary Algebra 112,College Level Math 55  ,Elementary Algebra 79,  ,Elementary Algebra 54,College Level Math 102  ,,College Level Math 54  

My problem is I don't know the number at the end or if it will be 2 or 3 digits.

I know how to flip it, if it was the exact same text everytime

sed -e 's/College Level Math,Elementary Algebra/Elementary Algebra,College Level Math/g'  

But the number variable has me at a loss

Answer by Mike Holt for if a string exist (including a variable), flip it using awk or sed


Use captures in your regular expression:

sed 's/\(College Level Math[^,]*\),\(Elementary Algebra[^,]*\)/\2,\1/'  

Edit: It has been pointed out to me on a previous answer of mine that escaping the parentheses in order to convert them from a literal to a metacharacter is not POSIX compliant and thus not as portable as it could be. If portability is a concern for you, you can omit the \ and just use the -r option to enable extended regular expression syntax, as in Jahid's answer.

Answer by Ed Morton for if a string exist (including a variable), flip it using awk or sed


$ awk 'BEGIN{FS=OFS=","} $2!="" && $3!=""{t=$2;$2=$3;$3=t} 1' file  ,Elementary Algebra 112,College Level Math 55  ,Elementary Algebra 79,  ,Elementary Algebra 54,College Level Math 102  ,,College Level Math 54  

Answer by Jahid for if a string exist (including a variable), flip

it using awk or sed

Using sed with extended regex:

sed -r 's/(College Level Math.*?),(Elementary Algebra.*)/\2,\1/g' filepath  

\1 is the first captured group (College Level Math.*?)

\2 is the second captured group (Elementary Algebra.*)

Thus the above sed performs a replacement operation from \1,\2 to \2,\1


The above regex will serve for general cases, but it will fail on cases like these:

,College Level Math 55,Elementary Algebra 111,Elementary Algebra 112  #\1=College Level Math 55,Elementary Algebra 111    ,College Level Math 55,College Level Math 55,Elementary Algebra 112  #\1=College Level Math 55,College Level Math 55  

So it's better not to use greedy regex like .*, instead if the entries are comma separated, you should use this:

sed -r 's/(College Level Math[^,]*),(Elementary Algebra[^,]*)/\2,\1/g' filepath  

Answer by alijandro for if a string exist (including a variable), flip it using awk or sed


another version

sed 's/\(,College Level Math [0-9]\{2,3\}\)\(,Elementary Algebra [0-9]\{2,3\}\)/\2\1/' urfile  

Answer by slayedbylucifer for if a string exist (including a variable), flip it using awk or sed


For given sample input, this should work:

$ sed -r 's/(,)(College Level Math)(.*)(Elementary Algebra)(.*)/\1\4\3\2\5/g' file

Above was GNU sed.

For other sed:

$ sed 's/\(,\)\(College Level Math\)\(.*\)\(Elementary Algebra\)\(.*\)/\1\4\3\2\5/g' file

It is capturing each line in 5 capture groups which are later back referenced as \1\4\3\2\5

Refer sed back referencing: http://www.grymoire.com/Unix/Sed.html#uh-4


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.