Parse "import a, b, c, d" into line-by-line expressions "import a\nimport b\nimport c\nimport d\n"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse "import a, b, c, d" into line-by-line expressions "import a\nimport b\nimport c\nimport d\n"
# 1  
Old 04-05-2014
Parse "import a, b, c, d" into line-by-line expressions "import a\nimport b\nimport c\nimport d\n"

First post. I'm just getting to grips with sed.
I've learned the basic substitution commands.
But I'm a bit stuck on this problem.

I'm running through some python files to convert syntax from Gtk2 to Gtk3 notation.

Consider a simple line of python like this ..

Code:
import gtk, pango, gtksourceview2, gobject

The terms might be in any order ...

e.g.
Code:
import gtk, gobject, pango, gtksourceview2
import gtk, pango, gobject, gtksourceview2, gobject

or even some terms left out in some input files ...

Code:
import gtk, gobject

I would like to see the output from sed as single line expressions ...

Code:
import gtk
import pango
import gtksourceview2
import gobject

and from there I can run through these single line expressions again to finally convert to Gtk3.

I've read how to use the "^, "|" , "&" and "$" operators in sed ...

Code:
sed -r 's/^import.(gtk$|pango$|gtksourceview2$|gobject$)/&\n/g' input.py > output.py

but I'm not there yet.

....

Here is a compressed code (Gtk2) example

Code:
import gtk, pango, gtksourceview2, gobject
import sys, os, re, glob, subprocess, webbrowser, base64, cgi, urllib2, shutil, time, pgsc_spellcheck

This is how the final (Gtk3) output code should look.

Code:
from gi.repository import Gtk
from gi.repository import Pango
from gi.repository import GtkSource
from gi.repository import GObject

import sys
import os
import re
import glob
import subprocess
import webbrowser
import base64
import cgi
import urllib2
import shutil
import pgsc_spellcheck

....

So the question as in title .. how to parse the line ...
Code:
import a, b, c, d, e

... (module names in any random order and some might be missing) into line-by-line expressions?

Thanks.

Last edited by bartus11; 04-05-2014 at 03:02 PM.. Reason: changed [quote] to [code].
This User Gave Thanks to scriptus For This Post:
# 2  
Old 04-05-2014
Try:
Code:
awk -F"," '/^import/{print $1;for (i=2;i<=NF;i++) print "import"$i;next}1' input.py

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-05-2014
Would this do:
Code:
sed '/import/ s/,/\nimport/g' file
import gtk
import pango
import gtksourceview2
import gobject
import sys
import os
import re
import glob
import subprocess
import webbrowser
import base64
import cgi
import urllib2
import shutil
import time
import pgsc_spellcheck

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-05-2014
@bartus11

Thanks for the suggestion but I prefer to stay with sed .. at least for now since I'm calling embedded sed from python subprocess. But I will try it out later.

@RudiC

That suggestion works great .. and in one line of code .. but I now have to analyse how it works. Don't know how I missed it in my reading about sed. I was trying overly complicated expressions.

Thank you both.

Now on to learning more sed (and awk I guess).
# 5  
Old 04-05-2014
Code:
$ awk '!/import/{$0 = "import"$0}1' RS=",|\n" file

import gtk
import pango
import gtksourceview2
import gobject
import sys
import os
import re
import glob
import subprocess
import webbrowser
import base64
import cgi
import urllib2
import shutil
import time
import pgsc_spellcheck

# 6  
Old 04-05-2014
@Akshay

Thanks. I'm now beginning to understand the different features of sed vs awk.

From an article I have just read (as a new member I can't post the link yet) ..

Code:
https://davidlyness.com/post/the-functional-and-performance-differences-of-sed-awk-and-other-unix-parsing-utilities

Quote:
awk's key feature being to automatically split input lines into their component fields.
sed and awk both work through as python subprocess call (executing command line in python).
# 7  
Old 04-05-2014
Quote:
Originally Posted by scriptus
@Akshay

Thanks. I'm now beginning to understand the different features of sed vs awk.

From an article I have just read (as a new member I can't post the link yet) ..

Code:
https://davidlyness.com/post/the-functional-and-performance-differences-of-sed-awk-and-other-unix-parsing-utilities



sed and awk both work through as python subprocess call (executing command line in python).
Well you are learning, awk splits line into field if you just specify FS Field separator, default FS is space.

$0 --> line
$1 --> first field/column
$2 --> second field/column
$NF --> last field/column in a line

Code:
$ echo "1 2 3 4 5" | awk '{print "First Field "$1; print "Last Field "$NF}'
First Field 1
Last Field 5

if you are using other than space, you have to use FS
Code:
$ echo "1,2,3,4,5" | awk -F, '{print "First Field "$1; print "Last Field "$NF}'
First Field 1
Last Field 5

Here you don't have to split each line into field as in python for example, awk does

Code:
for line in open("file"):
columns = line.split("\t")
print columns[1]


Last edited by Akshay Hegde; 04-05-2014 at 05:21 PM..
This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

4. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. Shell Programming and Scripting

Python 2.5 / 2.2 import "errno" fails on AIX Server

I am trying to import the "xmlrpclib" Module from Activepython 2.5 in einer older Python 2.2. Already achived this on a SUSE Linux server, but I am now required to do it on a AIX server. Resolved the first few error messages by copying files from Activepython to Python but I can't get the... (0 Replies)
Discussion started by: frieling
0 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question

Featured Tech Videos