awk -remove pattern from file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk -remove pattern from file
# 1  
Old 01-18-2016
awk -remove pattern from file

I have a file like this - I want to remove the 2015 (or any four digit #) from column $4 so I can get:
Code:
Nov 05 1997 /ifs/inventory2/

for example. Im not sure how. Should I use an if statement with awk?

Code:
Jan 16 2015 23:45 /ifs/sql_file
Jan 16 2015 23:45 /ifs/sql_file
Nov 05 2015 1997 /ifs/inventory2/
Nov 05 2015 1997 /ifs/inventory8/
Nov 05 2015 1997 /ifs/inventory7/
Nov 05 2015 1997 /ifs/inventory6/
Nov 05 2015 1997 /ifs/inventory5/

What I have got is this:
Code:
/tmp/filez1 |gawk '{if ($4 ~/1997/) }

but I don't know how to remove the 2015
from the file when 1997 is present.


I don't want anything removed from lines that do not have 1997
Moderator's Comments:
Mod Comment Using CODE tags isn't that hard. Please read the personal mail that received along with your infraction and review the tutorial that is included.

Last edited by Don Cragun; 01-18-2016 at 04:17 PM.. Reason: Change HTML tag to CODE tag to match closing CODE tag; add CODE and ICODE tags.
# 2  
Old 01-18-2016
Hello newbie2010,

Kindly use code tags as per forum rules for your commands/Inputs/codes which you are using into your posts, following may help you in same.
Code:
awk '{if($4 ~ /[0-9][0-9][0-9][0-9]/){$4="\b"}} 1'  Input_file

Output will be as follows.
Code:
Jan 16 2015 23:45 /ifs/sql_file
Jan 16 2015 23:45 /ifs/sql_file
Nov 05 2015 /ifs/inventory2/
Nov 05 2015 /ifs/inventory8/
Nov 05 2015 /ifs/inventory7/
Nov 05 2015 /ifs/inventory6/
Nov 05 2015 /ifs/inventory5/

Thanks,
R. Singh
# 3  
Old 01-18-2016
Try:
Code:
awk '$4~/^[0-9][0-9][0-9][0-9]$/{sub($4 FS,x)}1' file

or, if your awk supports this (gawk 3.x needs --re-interval):
Code:
awk '$4~/^[0-9]{4}$/{sub($4 FS,x)}1' file

or just:
Code:
awk '{sub(/^[0-9]{4} $/,x,$4)}1' file


--
Quote:
Originally Posted by RavinderSingh13
[..]
Code:
awk '{if($4 ~ /[0-9][0-9][0-9][0-9]/){$4="\b"}} 1'  Input_file

[..]
Try redirecting that to a file. You will notice that the backspace characters are still there, so I do not see how this can be a valid solution, other than that it looks OK on the terminal/screen...

Last edited by Scrutinizer; 01-18-2016 at 05:07 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-18-2016
Code:
perl -pae 's/ $F[3]// if $F[3] == "1997"' newbie2010.file

Code:
Jan 16 2015 23:45 /ifs/sql_file
Jan 16 2015 23:45 /ifs/sql_file
Nov 05 2015 /ifs/inventory2/
Nov 05 2015 /ifs/inventory8/
Nov 05 2015 /ifs/inventory7/
Nov 05 2015 /ifs/inventory6/
Nov 05 2015 /ifs/inventory5/

You may change the 1997 string to any other number you want.
# 5  
Old 01-19-2016
Not sure I understood the request entirely and correctly, but try
Code:
awk '$4 == "1997" {if ($3 == 2015) {$3=""; sub (FS FS, FS)}; print}' file
Nov 05 1997 /ifs/inventory2/
Nov 05 1997 /ifs/inventory8/
Nov 05 1997 /ifs/inventory7/
Nov 05 1997 /ifs/inventory6/
Nov 05 1997 /ifs/inventory5/

 
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 with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Beginners Questions & Answers

awk to remove pattern and lines above pattern

In the awk below I am trying to remove all lines above and including the pattern Test or Test2. Each block is seperated by a newline and Test2 also appears in the lines to keep but it will always have additional text after it. The Test to remove will not. The awk executed until the || was added... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to remove mutiple values from specific pattern, leaving a single value

In the awk below I am trying to remove all instances after a ; (semi-colon) or , (comma) in the ANN= pattern. I am using gsub to substitute an empty string in these, so that ANN= is a single value (with only one value in it the one right after the ANN=). Thank you :). I have comented my awk and... (11 Replies)
Discussion started by: cmccabe
11 Replies

4. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

5. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

6. Shell Programming and Scripting

Awk; pattern match, remove and re write

the following pattern match works correctly for me awk '/name="Fruits"/{f=1;next} /"name=Vegetables"/{f=0} f' filename This works well for me. Id like to temporarily move the match out of the file ( > newfile) and be able to stick it back in the same place at a later time. Is this... (7 Replies)
Discussion started by: TY718
7 Replies

7. Shell Programming and Scripting

awk to search for pattern and remove line

I am an awk beginner and need help figuring out how to search for a number in the first column and if it (or anything greater) exists, remove those lines. AM11400012012 2.26 2.12 1.98 2.52 3.53 3.01 3.62 5.00 3.65 7.95 0.79 3.88 0.00 AM11400012013 3.39 2.29 ... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

8. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

9. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

10. Shell Programming and Scripting

Need an awk / sed / or perl one-liner to remove last 4 characters with non-unique pattern.

Hi, I'm writing a ksh script and trying to use an awk / sed / or perl one-liner to remove the last 4 characters of a line in a file if it begins with a period. Here is the contents of the file... the column in which I want to remove the last 4 characters is the last column. ($6 in awk). I've... (10 Replies)
Discussion started by: right_coaster
10 Replies
Login or Register to Ask a Question