How to print range of a line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print range of a line?
# 1  
Old 12-13-2010
How to print range of a line?

Hi ,

I have a file content like following. In few lines I have fields with '"' and in few not.

Code:
"Country" character varying(150),
    "Region" character varying(60),
    "Total Page Requests" numeric,
    "Pages Served" numeric,
    "Ad request" numeric,
     Ads Served  numeric,
     NFR numeric,
    "Clicks" numeric,
    "CTR" numeric,
    "CPC" numeric,
    "Revenue" numeric,
    "eCPM" numeric

I want to print like following using awk or sed.

Code:
"Country"
    "Region" 
    "Total Page Requests"
    "Pages Served"
    "Ad request" 
     Ads Served 
     NFR 
    "Clicks" 
    "CTR" 
    "CPC"
    "Revenue" 
    "eCPM"

Please help me out.

Thanks in advance.

Last edited by radoulov; 12-13-2010 at 06:47 AM.. Reason: Code tags, please!
# 2  
Old 12-13-2010
Code:
sed 's/ *\(character\|numeric\).*$//' infile

For sed implementations that don't support alternation:

Code:
sed 's/ *character .*//; s/  *numeric.*//' infile

And of course, you should add other data types manually, if necessary.
This User Gave Thanks to radoulov For This Post:
# 3  
Old 12-13-2010
Code:
awk -F"character|numeric" '{print $1}' file

This User Gave Thanks to cabrao For This Post:
# 4  
Old 12-13-2010
Hi,
Thanks a ton for your quick response.
Is there any other way to make it dynamic. I don't want to add datatypes explicitly.

For eg. If the line contains quotes print the content along with quotes. Like - "Country"
else print the first column Like - NFR

Thank You
# 5  
Old 12-13-2010
thru sed..
Code:
sed -e 's/\(".*"\).*/\1/g' -e 's/ [^ ]*,//' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 12-13-2010
Thanks a lot.
This worked for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to print specific range using awk?

I want to print specific range of rows and then its columns using awk command. lets say if a file contain 15 line then i need to print line 5 to 9. awk '{print;for( NR>=5&&NR<=9); do; print "<tr>\n<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n</tr" done}' xyz.csv not sure what's wrong... (9 Replies)
Discussion started by: amyt1234
9 Replies

2. Shell Programming and Scripting

Print range of numbers

Hi I am getting an argument which specifies the range of numbers. eg: 7-15 Is there a way that i can easily (avoiding loop) print the range of number between and including the specified above. The above example should translate to 7,8,9,10,11,12,13,14,15 (3 Replies)
Discussion started by: tostay2003
3 Replies

3. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

4. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. Shell Programming and Scripting

awk to print range of fields

Hi file.in and file.out are in csv format. the code I have now is, cat file.in | awk -F"," '!($1$2$3$4$5$6$7$8 in a){a;print $0}' > file.out Here, I am printing entire line using $0. however, I want to print $1 to $150 and it should be in csv format. Cut -d is not good in performace.... (3 Replies)
Discussion started by: krishnix
3 Replies

8. Solaris

sed print range alongwith line numbers.

hi working with sed in a shell script using sed to print a range of lines from a given file for example to print lines 12-24 from a file sed 12,24p <filename> however i need to print the line numbers, alongwith the actual lines would this be possible at all? Thanks (1 Reply)
Discussion started by: xinuuser
1 Replies

9. Shell Programming and Scripting

Print Range Only Once Per File

Scenario: Each of several .txt files contain the following (but perhaps with some minor variations due to code version running on the devices from which the text was extracted): <output omitted> SWITCH1#show proc cpu hist 1... (4 Replies)
Discussion started by: svermill
4 Replies

10. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies
Login or Register to Ask a Question