Basic awk...newbie quetion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Basic awk...newbie quetion
# 1  
Old 06-29-2015
Basic awk...newbie quetion

Hi,

I was trying to change the value of the 4th column (put '1' in the 4th column of each row). My awk command is:

awk -F, '{$3=1;}1' OFS= input.txt > ./test_out.txt

My input file is:
Code:
a 1 2 31
b 4 5 61
c 7 8 91

My output file (test_out.txt)is:
Code:
a 1 2 31
b 4 5 61
c 7 8 91

What I actually want is:
Code:
a 1 2 1
b 4 5 1
c 7 8 1

What do I need to change in my awk code?

thanks!!
Moderator's Comments:
Mod Comment As noted many times before, please use CODE tags for sample input and output as well as for code sample.

Last edited by Don Cragun; 06-29-2015 at 05:11 PM.. Reason: Add missing CODE tags again.
# 2  
Old 06-29-2015
The field you are trying to change is number 4th
Code:
awk '{$4=1}1' input.txt > test_out.txt

There is no need to change the Field Separator to a comma (-F,) or the Output Field Separator (OFS= )
# 3  
Old 06-29-2015
Quote:
Originally Posted by pc2001
Hi,

I was trying to change the value of the 4th column (put '1' in the 4th column of each row). My awk command is:

awk -F, '{$3=1;}1' OFS= input.txt > ./test_out.txt

My input file is:
Code:
a 1 2 31
b 4 5 61
c 7 8 91

My output file (test_out.txt)is:
Code:
a 1 2 31
b 4 5 61
c 7 8 91

What I actually want is:
Code:
a 1 2 1
b 4 5 1
c 7 8 1

What do I need to change in my awk code?

thanks!!
Moderator's Comments:
Mod Comment As noted many times before, please use CODE tags for sample input and output as well as for code sample.
It looks like Aia may have already solved your problem, but I'm a little confused...

With the sample input given above, the code shown above would produce the output:
Code:
a 1 2 311
b 4 5 611
c 7 8 911

instead of what you showed us above.

And, it isn't just that you don't "need" to change FS and OFS; setting FS to comma (when your input field separator is a space and there are no commas in your input) means that each of your input lines contains one field instead of four. And setting OFS to an empty string removes all of your specified input field separators when you set any specific field.
# 4  
Old 06-30-2015
You can do it with just replacing the 4th column

awk '{print $1,$2,$3,"1"}' input.txt > output.txt
# 5  
Old 06-30-2015
Quote:
Originally Posted by Quijotes
You can do it with just replacing the 4th column

awk '{print $1,$2,$3,"1"}' input.txt > output.txt
Since the field being changed is being set to a non-zero numeric string (and also works if it is being set to a non-empty, non-numeric string), the simplest way to do it is:
Code:
awk '$4=1' input.txt > output.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Basic awk help

Im sure this is an easy question, but Ive tried and tried to get this to print all on one line and cant figure out why its not, so maybe someone can help awk '/AP/{sub(/:80/, "", $4);printf $4"\t"} /User-Agent/{sub(/^*:/,"");print};sub(/\.80/,"", $4);/Host/{sub(/^*:/,""); print}' What this... (10 Replies)
Discussion started by: sectech
10 Replies

2. Shell Programming and Scripting

basic awk questions

I find an script with awk sitting around. I went through some online manuals, but I can't figure out exactly how it works. I can't post the whole program. Not allowed. This is the line that is confusing me. I get when else is in the script grep -v "^REM " $1| grep -v "JUNK;" | awk -F" "... (2 Replies)
Discussion started by: guessingo
2 Replies

3. UNIX for Dummies Questions & Answers

some basic unix questions pls iam a newbie

what is a assembler what isa interpreter what is a compiler what is a socket what is a port what is meant by listening to a port how we do it how to know version and standard of unix one is using when one is on public access unix account how to see a jobid in... (1 Reply)
Discussion started by: scimitar
1 Replies

4. UNIX for Dummies Questions & Answers

Basic Newbie question

Hey guys I searched for this for the past few hours but maybe I dont know what the heck I am searching for. I am looking how to locate my .profile and where the heck I can find it to be able to edit it. Thanks in advance. (11 Replies)
Discussion started by: Koopa_Troopa
11 Replies

5. Shell Programming and Scripting

Help with awk for a newbie

Hi there, greetingt to everybody. I have configured syslog-ng to get messages over UDP saving logs into a text file, it works fine. I need to store the content of this file in several files depending from some criteria that I try to explain you with some examples : Suppose the content of my log... (5 Replies)
Discussion started by: capnino
5 Replies

6. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. Shell Programming and Scripting

awk basic issue

Hi all, I have an awk basic question. file.text Our Location: Our home has light yellow siding, and is a duplex on Main Street, across from the High School, and across the lane from the Health Center If I run: cat file.txt | awk '{print $2}' | grep... (7 Replies)
Discussion started by: research3
7 Replies

8. Shell Programming and Scripting

Need help with AWK (newbie)

Hello all I have a file with below data, I need to add a third column as XXXX if the first coloumn (H180620081) last number is "1" and YYYY if the first coloumn last number is "2" Can it be done with AWK, I would appreciate if anyone could help me ou with this. H180620081 31310 ... (11 Replies)
Discussion started by: udaybo
11 Replies

9. Programming

Basic questions on writing a Unix Service (newbie help!)

Hi there. I've got 12 years experience writing C++ on Windows, and 3 years C# on Windows. Now my boss wants me to write a C++ app to run on Unix as a multithreaded 'service' (i.e. a program that runs with no user intervention). Some quick questions for The Experts: * Whats the best C++... (3 Replies)
Discussion started by: Rutland Gizz
3 Replies

10. Shell Programming and Scripting

awk newbie

I would like to parse a file and use each record to create a unique file where the name of the file created is taken from the 3rd field. Example: here are 3 records. 000007 0110 07-0001583 20060802132213 20060801112601 000007 0110 07-0001584 ... (4 Replies)
Discussion started by: zoo591
4 Replies
Login or Register to Ask a Question