Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a .txt file with almost all of the airports in the world. Each line is a different airport, and the lines are in this format: City, Country - Airport Name (Airport Code)

ie:

Le Mans, France - Arnage (LME)
Detroit, USA - Willow Run (YIP)
San Francisco, USA - San Francisco International (SFO)

(the complete .txt is here: http://snipt.org/AnT8)

But I want in this format: "City, Country - Airport Name (Airport code)" => "Airport Code"

(yes, with quotes, it's for PHP coding)

ie:

"Le Mans, France - Arnage (LME)" => "LME"
"Detroit, USA - Willow Run (YIP)" => "YIP"
"San Francisco, USA - San Francisco International (SFO)" => "SFO"

How can I do this at once?

Thank you.

share|improve this question
    
sed -i -e 's/(\(...\))/(\1)" => "\1"/ -e 's/^/"/' file.txt It's too late for me to expand that into a proper answer w/explanation, but if anyone else wants to they should feel free. –  evilsoup Jun 9 '13 at 2:26
    
What OS are you using? –  terdon Jun 9 '13 at 3:03
    
The airport code seems to look like an unique identifier, are you sure you don't need "Airport Code" => "the whole line" instead? –  AnonymousLurker Jun 9 '13 at 3:05
    
@terdon I use Mac. –  Hugo Jun 9 '13 at 3:09
    
@AnonymousLurker Pretty sure, I need the identifier later. But thank you for the suggest. :) –  Hugo Jun 9 '13 at 3:09

4 Answers 4

up vote 5 down vote accepted

These work out of the box on Linux, OSX etc:

  1. Perl

    perl -ne 'chomp; /\((.+?)\)/; print "\"$_\" => \"$1\"\n"' list.txt
    

    more Perl, this one uses magic:

    perl -ane 'print "\"@F\" => \"$F[$#F]\"\n"' a
    
  2. awk/gawk

    awk -F '[()]' '{print "\""$0"\" =>\""$2"\""}' list.txt
    
  3. @evilsoup's sed

    sed  -e 's/(\(...\))/(\1)" => "\1"/' -e 's/^/"/' list.txt
    

None of these change the original file, use -i for the Perl and sed options or redirect the output if you want to save:

command list.txt > new_list.txt
share|improve this answer
2  
On OS X, if you want sed to do an in-place edit without saving a backup, the syntax is slightly different: sed -i '' ... (with GNU sed, the '' wouldn't be needed). –  Gordon Davisson Jun 9 '13 at 5:47
    
@GordonDavisson you mean that -i causes sed to NOT create a backup? In any case, since my (evilsoup's) sed command is quoted, the point is moot right? It will edit in place in OSX if you just add -i to the sed command as is? –  terdon Jun 9 '13 at 20:23
    
No, the -i option triggers an in-place edit and allows you to specify an extension for a backup file, for example sed -i.bkp .... If you want an in-place edit but don't want the backup, you specify a blank extension; in -i '' the double-quotes are an explicit empty string to tell sed there is no backup extension, and hence no backup. Note that this is completely separate from quoting the sed expression. –  Gordon Davisson Jun 9 '13 at 20:35
    
@GordonDavisson OK, I see, thanks. –  terdon Jun 9 '13 at 20:37

You can also use a graphical text editor with multi-line editing capabilities, such as Sublime Text.

  • Select all with CmdA, then turn on line-editing mode with ShiftCmdL.
  • Go to the end of the line with Cmd and copy the airport code in the parentheses. Then wrap the line in quotes, add the => and paste the code again.
  • Press Esc to exit line-editing mode.

So basically, you can just do the same like you'd do with one line.

share|improve this answer
    
For me this is the easiest answer. Thank you! –  Hugo Jun 9 '13 at 20:15

Emacs does have the ability to use multiple cursors. That way, you could directly edit all of the lines at the same time. The video on setting this up is on the Emacs Rocks website http://emacsrocks.com/e13.html .

share|improve this answer

You could copy-paste it into a spreadsheet... then split the lines into columns via delimiter... when the columns have been made as "City, Country - Airport Name (Airport code)" ... simple add another column with repeated string: => and copy the "Airport code" column to it next..

With that it could seem like this:

"City, Country - Airport Name (Airport code)" => "Airport Code"

Just copy paste all table contents into notepad or whatever text editor you have for mac... and in that edior, replace TAB with a SPACE or NOTHING, as per your code requirements..

you'll only require a spreadsheet software like MS Excel, and a text editor like notepad.. I'm a windows user, so don't know about softwares for mac...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.