What awk 1-liner will replace value in 1stField of a delimited file with the value of '5' ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What awk 1-liner will replace value in 1stField of a delimited file with the value of '5' ?
# 1  
Old 05-30-2013
What awk 1-liner will replace value in 1stField of a delimited file with the value of '5' ?

Hi,




I am a newbie to awk. Here is my problem. Looking for an awk 1-liner to solve it:

My Computing Environment:

- Solaris10
- I prefer to use csh or sh shells



1. Lets say my input file is File1.dat (delimter = | ) and looks as follows:

(File1.dat)

Code:
2|Harry|California|blue|
3|John|Nevada|green|
4|Mary|Arizona|orange|
8|Jack|Utah|yellow|


2. I want to create a 2nd file (an output file) named File2.dat, where i replace the value in the 1stField of every line with the value of '5', so that the File2.dat looks as follows:

(File2.dat)

Code:
5|Harry|California|blue|
5|John|Nevada|green|
5|Mary|Arizona|orange|
5|Jack|Utah|yellow|


Thanks in advance,

Notice the last delimiter | at the end of each record.

If you could please let me know what part of the awk-1liner syntax refers to the FieldNumber (in this case Field1) where we are doing this value-replace operation

Andy B

Last edited by Scrutinizer; 05-31-2013 at 02:02 AM.. Reason: code tags
# 2  
Old 05-30-2013
Code:
/usr/xpg4/bin/awk -F\| '{$1=5}1' OFS=\| File1.dat > File2.dat

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Replace a number in the last line of a delimited file.

Hi all, I am fairly new to UNIX and I was wondering if you could provide me with some help! Lets say i have a file as below : Line 1 Line 2 Line 3 ABC|12|4|2 Now the number 4 in bold, this number will represent the number of row there is in the file excluding the header and footer... (10 Replies)
Discussion started by: Stinza
10 Replies

3. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

4. Shell Programming and Scripting

Replace ^M and the new line that follows it in a delimited file

Hello, I want to Replace/Remove ^M and the new line that follows it in a delimited file. So far I have tried following and nothing seems to work for me . Tr –d ‘\r\n’ < old.dat > new.dat -removes all the linefeed, not just the ones after a ^M. Sed ‘/^M$/{N; s/.\n//;}’ < old.dat >... (7 Replies)
Discussion started by: bluestarmoon
7 Replies

5. Shell Programming and Scripting

Replace col 23 - 26 with new value, non delimited file

hello, i have a undelimited file which contains 229 byte records. i want to change column 23 - 26 with a new value and also change the sign of the data in colulmn 30 - 70. i've tried SED for the first change, but nothing happens: sed 's/\(^.\{22\}\).\{4\}\(.*\)/\0603\2/' inputfile heres an... (8 Replies)
Discussion started by: blt123
8 Replies

6. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

7. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

8. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

9. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

10. 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
Login or Register to Ask a Question