Using sed to format several fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to format several fields
# 1  
Old 10-17-2010
Using sed to format several fields

I have data that is tab delimited and looks like:

Code:
/dev/dsk/c0t0d0s1       -       -       swap    -       no      -
/dev/dsk/c0t0d0s0       /dev/rdsk/c0t0d0s0      /       ufs     1       no      -
/dev/dsk/c0t0d0s6       /dev/rdsk/c0t0d0s6      /usr    ufs     1       no      -
/dev/dsk/c0t0d0s4       /dev/rdsk/c0t0d0s4      /var    ufs     1       no      -
/dev/dsk/c0t0d0s5       /dev/rdsk/c0t0d0s5      /local  ufs     2       yes     -
/dev/dsk/c0t0d0s3       /dev/rdsk/c0t0d0s3      /usr/openwin    ufs     2       yes     -
/dev/dsk/c1t0d0s0       /dev/rdsk/c1t0d0s0      /opt    ufs     3       yes     -
/dev/dsk/c1t0d0s3       /dev/rdsk/c1t0d0s3      /export/user1   ufs     3       yes     nosuid
/dev/dsk/c1t1d0s0       /dev/rdsk/c1t1d0s0      /usr/local      ufs     3       yes     -

And I am supposed to use sed to show the device (first only -- /dev/dsk/) and file system (ufs or ext2). So the solution would look like:

Code:
/dev/dsk/c1t1d0s0      ufs

So far my solution is:

Code:
/^\/dev\/dsk/!d
/^\/dev\/dsk/s/\/dev\/rdsk\/c[0-9]t[0-9]d[0-9]s[0-9]//g
s/              /       /

Which yeilds the following output:

Code:
/dev/dsk/c1t1d0s0      /usr/local      ufs     3       yes     -

I am getting closer but I feel like I am taking the wrong approach here.

Any suggestions? Thanks in advance.
# 2  
Old 10-17-2010
maybe df -k -F ufs give you the direct result.
This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 10-17-2010
That would probably be a better option in the real world. This, however, is a homework problem due Wednesday. I'll ask tomorrow and post the solution but I was hoping to have it done before then. It's driving me crazy.
# 4  
Old 10-17-2010
Code:
sed 's/\([^      ][^      ]*\)    [^      ][^     ]*      [^      ][^     ]*      \([ue][xf][^     ]*\).*/\1       \2/' myfs

Code:
awk '$4 ~ /ufs|ext2/ {print $1"       "$4}' myfs

# 5  
Old 10-18-2010
ctsgnb,

I haven't tested your awk solution, but your sed solution doesn't seem to work on my end.

I've played around with your idea of back-referencing though, and have come up with this:

Code:
s/\(\/dev\/dsk\/c[0-9]t[0-9]d[0-9]s[0-9]*\) .* \(ufs\) .* /\1 \2/

Individually they work, but when I combine the two search patterns together it doesn't produce the right output.

Any thoughts anyone?

---------- Post updated 10-18-10 at 12:40 AM ---------- Previous update was 10-17-10 at 11:58 PM ----------

Turns out one working solution is as follows:

Code:
/^$/d
/^[^/]/d
/^\/p/d
s/	[^	][^	]*	[^	][^	]*	[^	][^	]*[	]*$//
s/	[^	][^	]*	[^	][^	]*//

...FML
# 6  
Old 10-18-2010
If you want to use sed
Code:
sed 's/\t.*\(\t[^\t]*\)\(\t[^\t]*\)\{3\}/\1/' infile

But perhaps awk is more suited (see ctsgnb's suggestion)

Last edited by Scrutinizer; 10-18-2010 at 02:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to format file and combine two fields using comma

I am trying to use awk to format the file below, which is tab-delimited. The desired out is space delimited and is in the order of $9 $13 $2 $10-$11.$10 and $11 are often times multiple values separated by a comma, so the value in $10 is combined with the first value from $11 using the comma.... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Format Fields

I have a CSV file like this: abc ,def ghi ,jkl mno ,pqr stu ,vwx Desired Output: Field 1: abc mno Field 2: def ghi pqr stu Field 3: jkl vwx This does not work: for i in `cat file.txt` do Field1=`echo $i | awk -F ',' '{print $1}'| sed 's/^ *// ; s/ *$//'`... (8 Replies)
Discussion started by: shellguy
8 Replies

3. Homework & Coursework Questions

Swapping Fields with Sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The assignment is to convert a text table to csv format. I've got the cleaning up done, but I need to swap two... (0 Replies)
Discussion started by: VoiceInADesert
0 Replies

4. Shell Programming and Scripting

awk,cut fields by change field format

Hi Everyone, # cat 1.txt 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 # cat 1.txt | awk -F, '{OFS=",";print $1,$3,$4,$5}' 1321631,19,20091001011859,20091001011907... (7 Replies)
Discussion started by: jimmy_y
7 Replies

5. UNIX for Dummies Questions & Answers

Fields in csv files using sed

Hi, I am working right now with a csv file and I want to insert an excel formula say to the 6th column. sample csv file: 1234,lag,0,77,544,,7 1234,lag,222,0,7,,7 at first i used a simple command: sed 's/^\(.\{17\}\)/\1word/' file.csv but the result is this: ... (2 Replies)
Discussion started by: paoie
2 Replies

6. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies

7. UNIX for Advanced & Expert Users

Format problems with fields

The following output has a space as the Field Separator. I need: $1 Set the field width to 15 then zero-fill to the right. $6 Set the field width to 15 then zero-fill to the left. 01-10016 1000 MV010 20090708 12003 $NK0015101 01 01-100161 12000 MV070 20090708 12003 $NK0015201 01... (6 Replies)
Discussion started by: talk2pawee
6 Replies

8. Shell Programming and Scripting

manipulating Fields in file using SED

Hi, I have two issues: I have one file say file1.dat and its over 3GB. It contains pipe delimited fields. The first line in the file is the header field which tells the column names etc. and from second line it's the data fileds with pipe delimited. Something like below: ... (5 Replies)
Discussion started by: rkumar28
5 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. Shell Programming and Scripting

sed command to edit fields

Hi, I'm a newbie to sed and I'm having trouble working with sed and fields. Suppose I have a text file with: AAA RFG:$2.10:6:25Oct06 WDD GGTR:$3.50:5:25Oct06 ADDSJ OO:$1.37:3:26Oct07 UGBDN S:$4.73:1:27Oct06 USY ADC:$2.38:20:27Oct06 And I want to substitute field 2 of line 3 with, say,... (3 Replies)
Discussion started by: aloe_vera
3 Replies
Login or Register to Ask a Question