Find in first column and replace the line with Awk, and output new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find in first column and replace the line with Awk, and output new file
# 1  
Old 11-07-2011
Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file

File1.txt
Code:
"2011-11-02","Georgia","Atlanta","x","",""
"2011-11-03","California","Los Angeles","x","",""
"2011-11-04","Georgia","Atlanta","x","x","x"
"2011-11-05","Georgia","Atlanta","x","x",""    
"2011-11-06","Georgia","Atlanta","x","",""
"2011-11-07","Rhode Island","Providence","x","",""

***OUTPUT***
Result.txt
Code:
"2011-11-02","Georgia","Atlanta","x","",""
"2011-11-03","California","Los Angeles","x","",""
"2011-11-04","Georgia","Atlanta","x","x","x"
"2011-11-05","Georgia","Atlanta","x","x","x"
"2011-11-06","Georgia","Atlanta","x","",""
"2011-11-07","Rhode Island","Providence","x","",""

I am looking for 2011-11-05 in File1.txt, and I want to put an x on the 6th column. I figure we can use awk to find 2011-11-05, and replace the row with:
"2011-11-05","Georgia","Atlanta","x","x","x" an output a new Result.txt file

Also another awk script, to find 2011-11-03, put an x on the 5th and 6th column.
"2011-11-03","California","Los Angeles","x","x","x"

Thank you.
# 2  
Old 11-07-2011
Code:
awk '/2011-11-05/ {$6=$5}1' FS=, OFS=, File1.txt

You can refer it to finish your second request.
This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 11-07-2011
Thanks so much!!

So for the second it would be.
Code:
awk '/2011-11-03/ {$5=$4; $6=$4}1' FS=, OFS=, File1.txt

It works, just need to confirm this is the correct syntax with a semicolon?

; too run two commands
# 4  
Old 11-07-2011
Quote:
Originally Posted by charles33
Thanks so much!!

So for the second it would be.
Code:
awk '/2011-11-03/ {$5=$4; $6=$4}1' FS=, OFS=, File1.txt

It works, just need to confirm this is the correct syntax with a semicolon?

; too run two commands
You are right!...

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 11-08-2011
Thanks

Thanks.

I am learning. Thanks for rdcwayx and ahamed101 for being a great teacher!

By the way Ahamed, I am still testing the last code you gave me. It all works perfect! I will post as soon as it goes into production.

https://www.unix.com/shell-programmin...using-awk.html

Thanks Again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Find and replace last line in a file

Hi I am having a file which has like this content shown below Aaa,bb,cc,dd Xxx,yy,d,12 Dodd,12-Jun,t I need to replace last line like this Aaa,bb,cc,dd Xxx,yy,d,12 Dodd,10-August,t (13 Replies)
Discussion started by: rohit_shinez
13 Replies

4. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

5. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

6. Shell Programming and Scripting

find and replace a line in a file

Hi, I am want find and replace in following content in the file. i want to repalce a word TABLESPACE XCRM_<ANY_CHAR> to TABLESPACE XCRM Sample File to Replace : LOB(COMPLEX_VALUE) STORE AS ( TABLESPACE XCRM_MED_D_NEW STORAGE(INITIAL 64K BUFFER_POOL DEFAULT) ENABLE... (3 Replies)
Discussion started by: gavemani
3 Replies

7. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

8. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

9. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

10. Shell Programming and Scripting

Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell. Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same... (3 Replies)
Discussion started by: jclanc8
3 Replies
Login or Register to Ask a Question