Replace line and field using SED and/or AWK or some other suggestion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace line and field using SED and/or AWK or some other suggestion
# 1  
Old 02-20-2012
Replace line and field using SED and/or AWK or some other suggestion

QUESTION 1:
How do you replace a specific line (i.e. line 4) with a new user defined line (i.e. the contents of SAMS’s name, history, math and English grades have been set already). I have been attempting to use SED (FYI: I don’t have GNU SED) or AWK, but haven’t had any luck. FYI: I am using SOLARIS 10 as an OS. See example below. FYI: KEEP IN MIND FOR ALL EXAMPLES I WAS UNABLE TO PROPERLY ALIGN NAME, HISTORY, MATH AND ENGLISH WITH THERE CORRESPONDING VALUES USING THIS THREAD WORD PROCESSOR .
Below is the starting file.

NAME: HISTORY: MATH: ENGLISH:
ROB A B A
DAN A C B
JOHN C B A

For example if John has left the class and SAM has joined and his grades are all A's.
Resulting file (BOLD TEXT IS WHAT NEEDS TO CHANGE):

NAME: HISTORY: MATH: ENGLISH:
ROB A B A
DAN A C B
SAM A A A





Question 2:
Does anyone know how to replace a specific field (i.e. line 2, field 2)? I would like to do the replacement this generically (line 2, field 2). So, for example I don’t want to use grep on ROB. Again, I do not have GNU SED. See example below.
Below is the starting file.

NAME: HISTORY: MATH: ENGLISH:
ROB A A A
DAN A A A
JOHN A A A

Resulting file (BOLD TEXT IS WHAT NEEDS TO CHANGE):

NAME: HISTORY: MATH: ENGLISH:
ROB A B A
DAN A A A
JOHN A A A

Last edited by methyl; 02-20-2012 at 11:49 PM.. Reason: Clearly homework and should have been posted in the homework forum
# 2  
Old 02-20-2012
Code:
[root@node2 ~]# cat file1
ROB A B A
DAN A C B
JOHN C B A
[root@node2 ~]# sed 's/JOHN.*/SAM A A A/g' file1
ROB A B A
DAN A C B
SAM A A A
[root@node2 ~]# cat file2
ROB A A A
DAN A A A
JOHN A A A
[root@node2 ~]# sed '/ROB/s/A/B/2' file2
ROB A B A
DAN A A A
JOHN A A A

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

3. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

4. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

sed or awk to replace a value in a certain line containing a string

hi experts , I have an input like following. R sfst 1000.0000 $ new time step for mass scaled calculation R dt2ms -4.000E-7 $ friction value for blank R mue ... (10 Replies)
Discussion started by: hamnsan
10 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line.

I have an input like following. *DEFINE_CURVE_TITLE Force for tool binder $# lcid sidr sfa sfo offa offo dattyp 3 0 1 .000000 125.00000 0.000 0.000 0 $# a1 ... (5 Replies)
Discussion started by: hamnsan
5 Replies

7. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

8. UNIX for Dummies Questions & Answers

howto replace field in line

Hi all, I'm writing a script in bash where i'm reading line by line into variable, and the i want to replace the second filed in the line with another number, for example if the line looks like this: 10 20 30 40 50 it should look like this: 10 90 30 40 50 and put the new result inside... (1 Reply)
Discussion started by: morfix
1 Replies

9. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies

10. UNIX for Dummies Questions & Answers

Replace password field using ed/sed

I need to edit the password file to change the password field to *LK* for a specified account (abctest) like: abctest:*LK*:135:20::/home/abctest:/sbin/sh Can anyone help me do this using ed or sed? Thanks a lot! (6 Replies)
Discussion started by: munch
6 Replies
Login or Register to Ask a Question