awk command 2nd col remove the '-' value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command 2nd col remove the '-' value
# 1  
Old 01-05-2016
awk command 2nd col remove the '-' value

Hi,

i was tried using the awk command for replacing '-' in the second column. but the below command replacing the entire file.

Code:
cat 1.txt |awk '{gsub(/-/,"")}1'

Input file
Code:
1,2,3,-4,5,6
1,-2,3,4,5,-6
1,2,3,4,5,6
1,-2,3,4,-5,6

Output file
Code:
1,2,3,-4,5,6
1,2,3,4,5,-6
1,2,3,4,5,6
1,2,3,4,-5,6


Last edited by Franklin52; 01-05-2016 at 08:28 AM.. Reason: Adding code tags
# 2  
Old 01-05-2016
Hello onesuri,

Following may help you in same.
Code:
awk '{sub(/\-/,X,$2);} 1' FS=, OFS=,  Input_file

Output will be as follows.
Code:
1,2,3,-4,5,6
1,2,3,4,5,-6
1,2,3,4,5,6
1,2,3,4,-5,6

Also for your code following are the things which you could correct.
i- awk could read Input_file by itself so no need to use cat.
ii- gsub(/-/,"") means it will substitute match for all matches so to restrict it to a very first match you could use sub with only $2 as you needed to replace values in 2nd field only. Also in case we have more than 1 occurrence of - in $2 then we could use gsub(/-/,X,$2).
iii- BY default awk uses space as a field separator so here you should mention field separator either by -F"," or by FS=, so that it will make , as field separator.

Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-05-2016 at 08:51 AM.. Reason: Adding some comments to explain user's command which user used to get output.
# 3  
Old 01-05-2016
Or
Code:
awk -F, '$2<0{$2=-$2}1' OFS=, file
1,2,3,-4,5,6
1,2,3,4,5,-6
1,2,3,4,5,6
1,2,3,4,-5,6

# 4  
Old 01-05-2016
Got Perl?

Code:
perl -pe 's/^(\d+,)-/$1/' onesuri.file

Got sed?

Code:
sed 's/^\([^,]*,\)-/\1/' onesuri.file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying col values based on another col

Hi, Please help with this. I have several excel files (with and .xlsx format) with 10-15 columns each. They all have the same type of data but the columns are not ordered in the same way. Here is a 3 column example. What I want to do add the alphabet from column 2 to column 3, provided... (9 Replies)
Discussion started by: newbie83
9 Replies

2. Shell Programming and Scripting

Printing from col x to end of line, except last col

Hello, I have some tab delimited data and I need to move the last col. I could hard code it, awk '{ print $1,$NF,$2,$3,$4,etc }' infile > outfile but it would be nice to know the syntax to print a range cols. I know in cut you can do, cut -f 1,4-8,11- to print fields 1,... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

3. Shell Programming and Scripting

awk - getting uniq count on multiple col

Hi My file have 7 column, FIle is pipe delimed Col1|Col2|col3|Col4|col5|Col6|Col7 I want to find out uniq record count on col3, col4 and col2 ( same order) how can I achieve it. ex 1|3|A|V|C|1|1 1|3|A|V|C|1|1 1|4|A|V|C|1|1 Output should be FREQ|A|V|3|2 FREQ|A|V|4|1 Here... (5 Replies)
Discussion started by: sanranad
5 Replies

4. Shell Programming and Scripting

Merging data from col 3 to col2 with awk or sed

Dear Friends, I have a file in which lists State and Phone numbers. Does anybody have a solution in which to take the data from col3 and place it on col2? AK 2988421640 9077467107 AK 2998266711 2069239034 AK 2983804242 2069239034 AK 2960407849 AK ... (3 Replies)
Discussion started by: liketheshell
3 Replies

5. UNIX for Advanced & Expert Users

Print line based on highest value of col (B) and repetion of values in col (A)

Hello everyone, I am writing a script to process data from the ATP world tour. I have a file which contains: t=540 y=2011 r=1 p=N409 t=540 y=2011 r=2 p=N409 t=540 y=2011 r=3 p=N409 t=540 y=2011 r=4 p=N409 t=520 y=2011 r=1 p=N409 t=520 y=2011 r=2 p=N409 t=520 y=2011 r=3 p=N409 The... (4 Replies)
Discussion started by: imahmoud
4 Replies

6. Shell Programming and Scripting

Awk to print distinct col values

Hi Guys... I am newbie to awk and would like a solution to probably one of the simple practical questions. I have a test file that goes as: 1,2,3,4,5,6 7,2,3,8,7,6 9,3,5,6,7,3 8,3,1,1,1,1 4,4,2,2,2,2 I would like to know how AWK can get me the distinct values say for eg: on col2... (22 Replies)
Discussion started by: anduzzi
22 Replies

7. HP-UX

changing col(1) command stdout as fully buffered?

Hi All, I am talking about unix col(1) command used for some reverse line filtering etc. And I notice that the stdout of this command is line buffered i.e. the stdout will flush the data in its buffer line by line. So the number of writes performed by stdout are more. So now if I make stdout... (0 Replies)
Discussion started by: sunilsbjoshi
0 Replies

8. UNIX for Dummies Questions & Answers

Info about col command

Hello every1. Can any1 help me with the col command. Wat is a reverse line feed. Which kind of files u need to use the col command. (2 Replies)
Discussion started by: rahulrathod
2 Replies

9. UNIX for Advanced & Expert Users

changing col(1) command stdout as fully buffered?

Hi All, I am talking about unix col(1) command used for some reverse line filtering etc. And I notice that the stdout of this command is line buffered i.e. the stdout will flush the data in its buffer line by line. So the number of writes performed by stdout are more. So now if I make stdout... (0 Replies)
Discussion started by: sunilsbjoshi
0 Replies

10. UNIX for Advanced & Expert Users

Help On col command

Hello Can Any1 tell me the difference between the col command and the col command with the -f option. I tried running both of them but i can't see any difference. Please guide me. (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question