Sorting and extracting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting and extracting
# 1  
Old 10-09-2012
Sorting and extracting

I would like to know aside from while or for loop statement.
how can i extract i file like this:

File:
Code:
name1$%<name1#first>; name2$%<name2#second>; name3$%<name3#third>

Output:
Code:
name1#first
name2#second
name3#third

Thanks

Last edited by Franklin52; 10-09-2012 at 03:05 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-09-2012
Code:
sed 's/[^<]*<\([^>]*\)>;\? \?/\1\n/g' file

These 2 Users Gave Thanks to balajesuri For This Post:
# 3  
Old 10-09-2012
thanks a lot..

---------- Post updated at 11:53 PM ---------- Previous update was at 11:52 PM ----------

care to explain ? the syntax?

---------- Post updated 10-09-12 at 12:05 AM ---------- Previous update was 10-08-12 at 11:53 PM ----------

how about this, put exclamation before the number sign?

OUTPUT:
Code:
name1!#first
name2!#second
name3!#third


Last edited by Franklin52; 10-09-2012 at 03:05 AM.. Reason: Please use code tags for data and code samples
This User Gave Thanks to kenshinhimura For This Post:
# 4  
Old 10-09-2012
Code:
sed 's/[^<]*<\([^>]*\)>;\? \?/\1\n/g' file

can anyone explain the above?

Last edited by Franklin52; 10-09-2012 at 03:05 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 10-09-2012
with awk.....

Code:
awk -F "[<>]" '{ for(i=2;i<=NF;i+=2){sub("#","!#",$i);print $i}}' file


Last edited by pamu; 10-09-2012 at 03:40 AM..
# 6  
Old 10-09-2012
Quote:
Originally Posted by dhilipan123
Code:
sed 's/[^<]*<\([^>]*\)>;\? \?/\1\n/g' file

can anyone explain the above?
Code:
1) [^<]*     --> 0 or more occurrences of a character which is not a <
2) <         --> literal <
3) \([^>]*\) --> 0 or more occurrences of a character which is not a >
4) ;\?       --> optional ; character (0 or 1 occurrence)
5)  \?       --> optional space character (0 or 1 occurrence)
6) \1        --> string matched by the pattern in 3 (\( and \) are used to save the string matched by the pattern)
7) \n        --> newline

But, this might not work with all sed implementations. If you want something which works on most (if not all) implementations, use a slight modification of the above sed command:
Code:
sed 's/[^<]*<\([^>]*\)>;\{0,1\} \{0,1\}/\1\
/g' file

This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 10-09-2012
in perl

Quote:
perl -ne ' @arr = split /[<>]/ ; print "$arr[1] \n$arr[3] \n$arr[5]\n" ' filename
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help sorting with du

Hi! As a part of a larger script I'm using the comand du -k to get the sizes of various directories, the output is printed like this: 1324 ./images/backup/2012 1333 ./images/backup 1400 ./images 22 ./video/backup 44 ./video 1450 . I'm trying to print it the other way around with... (2 Replies)
Discussion started by: Tralaraloro
2 Replies

2. Red Hat

Sorting

Hi guys, i have a prblm in unix scripting can anyone of you figure it out for me?? The question is i need to store the parent processid and processid in a file and using shell scripting, if i enter the parent processid it should display the processid. one more... (5 Replies)
Discussion started by: rajeshb6
5 Replies

3. Shell Programming and Scripting

sorting

Hii guys, I need to sort my file and remove duplicates before writing to another file. The first line in the file are column names. I dont want this line to be sorted and should always be the first line in the output. sort -u file.txt > file1.txt. is the command that i am using... (4 Replies)
Discussion started by: just4u_sharath
4 Replies

4. Shell Programming and Scripting

sorting help

Hi, Please i need help in writing an 'awk' script in sorting the following data; traceroute6 to 2001:1ba0:2a0:5965:0:30:24:1 (2001:1ba0:2a0:5965:0:30:24:1) from 2001:418:1::62, 64 hops max, 16 byte packets 1 2001:418:1::4 0.342 ms 2 2001:418:1::1 0.630 ms 3 2001:504:16::1b1b 0.393 ms 4... (6 Replies)
Discussion started by: sam127
6 Replies

5. Shell Programming and Scripting

sorting

Hi all, Does anyone can help me the following question? I would like to write an AWK script. In the following input file, each number in "start" is paired with numbers in column "end". No Start End A 22,222,33,22,1233,3232,44 555,333,222,55,1235,3235,66... (7 Replies)
Discussion started by: phoeberunner
7 Replies

6. Shell Programming and Scripting

Sorting HELP

Hi, I have posted related topic but as i continue the research I find more need to sort the data. AS(2607:f278:4101:11:dead:beef:f00f:f), AS786 AS6453 AS7575 AS7922 AS(2607:f2e0:f:1db::16), AS786 AS3257 AS36252 AS786 AS3257 AS36252 AS(2607:f2f8:1700::2), AS786 AS6939 AS25795 ... (6 Replies)
Discussion started by: sam127
6 Replies

7. UNIX for Advanced & Expert Users

HELP on sorting

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this; 192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible... (1 Reply)
Discussion started by: sam127
1 Replies

8. Homework & Coursework Questions

Sorting help

i have list of files: Wang De Wong CVPR 09.pdf Yaacob AFGR 99 Second edition.pdf Shimon CVPR 01.pdf Den CCC 97 long one.pdf Ronald De Bour CSPP 04.pdf ..... how can i sort this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its... (1 Reply)
Discussion started by: nirnir26
1 Replies

9. UNIX for Dummies Questions & Answers

Sorting help

i have list of files: Wang De Wong CVPR 09.pdf Yaacob AFGR 99 Second edition.pdf Shimon CVPR 01.pdf Den CCC 97 long one.pdf Ronald De Bour CSPP 04.pdf ..... how can i sort this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its... (1 Reply)
Discussion started by: nirnir26
1 Replies

10. UNIX for Dummies Questions & Answers

Help: Sorting and extracting a line from a file

I was proposed with the following problem: The file 'numbers' contains a list of numbers. Write a command to place the largest one of those numbers in the file 'largest' (there should be nothing else in that file). Do not use the 'head' command in your answer. I think if i used the sort... (1 Reply)
Discussion started by: stava
1 Replies
Login or Register to Ask a Question