sed/awk to update 1st column if condition met


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed/awk to update 1st column if condition met
# 1  
Old 10-27-2010
sed/awk to update 1st column if condition met

Hi,

I am trying to update the 1st column of a file but only if it contains a char

here is an example of my file

Code:
1111aaa    9999      textaaa
22222bbb   9999      textbbb
3333       9999      textccc
444ddd     9999      textddd

i would like the output to remove any characters ([A-Z][A-Z][A-Z]) from the 1st column only. i do not want the other columns to be updated.

I have tried to use sed like
Code:
sed 's/[A-Z][A-Z][A-Z]/   /'

I also tried to pipe in the first column using something like
Code:
awk '{print $1}' filename.txt

but can't get it to work properly.

here is what i get
Code:
1111       9999      textaaa
22222      9999      textbbb
3333       9999      text
444        9999      textddd

but i want it to be like this:
Code:
1111       9999      textaaa
22222      9999      textbbb
3333       9999      textccc
444        9999      textddd

can this be done easily using sed/awk or would perl be a better option?

Last edited by Scott; 10-27-2010 at 03:44 PM.. Reason: Please use code tags
# 2  
Old 10-27-2010
Code:
$ cat file
1111aaa 9999 textaaa
22222bbb 9999 textbbb
3333 9999 textccc
444ddd 9999 textddd
$ sed "s/^\([^ ]*\)[a-z]\{3\}/\1/" file
1111 9999 textaaa
22222 9999 textbbb
3333 9999 textccc
444 9999 textddd

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 10-27-2010
Hi,

Code:
sed -e 's/\([1-9]*\)\([a-z]*\) \(.*\)/\1 \3/' input_file

# 4  
Old 10-28-2010
Hi,

Another solution. I suppose there are exactly three characters in first column and at least one number ahead.
Code:
sed 's/^\([1-9]\+\)\([a-zA-Z]\{3\}\)/\1   /' infile

Regards,
Birei
# 5  
Old 10-28-2010
Code:
awk '{gsub(/[a-zA-Z]/,"",$1)}1' OFS="\t" infile

# 6  
Old 10-28-2010
Code:
awk '{sub($1,int($1))}1' file

to keep the original format.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

2. Shell Programming and Scripting

awk to update file with numerical difference if condition is met

In the file1 below if $9 and $12 are . (dot) then the value in $8 of file1 is used as a key (exact match) to lookup in each $2 of file2, when a match is found then the value of $4 in file1 is used to look for a range match within +/- 50 using the values in $4 and after in file2. The number of... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Need help on how to append on the filename when condition met.

Hi All, Seeking for your assistance on how to append the specific string when $3 condion met. ex. file1.txt ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 expected output:... (5 Replies)
Discussion started by: znesotomayor
5 Replies

5. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

6. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

7. Shell Programming and Scripting

Comparing all lines in a column with another is condition is met

Sorry for this noob question, I have file with 4 columns like where columns 2 and 4 have numbers a 55 k 3 b 59 l 3 c 79 m 277 d 255 n 277 e 257 o 267 f 267 p 287 g 290 q 287 h 290 r 287 i 310 s 900 now i want to select only those rows, where values in column 4 are greater than... (4 Replies)
Discussion started by: amits22
4 Replies

8. Shell Programming and Scripting

Delete if condition met in a column

i have a table like this: id, senderNumber, blacklist ----------------------------- 1 0835636326 Y 2 0373562343 Y 3 0273646833 Y and I want to delete automatically if a new inserted row on another table consist anything on senderNumber column above using a BASH Script I... (9 Replies)
Discussion started by: jazzyzha
9 Replies

9. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

10. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies
Login or Register to Ask a Question