Find a line using a condition and replace a string in that line

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Find a line using a condition and replace a string in that line
# 1  
Old 12-14-2011
Data Find a line using a condition and replace a string in that line

Hello,
I have a 100 line code. I have given a sample of it below:
Code:
ABC*654654*1*54.54*21.2*87*1*654654654654
CCC*FS*FS*SFD*DSF
GGG*FGH*CGB*FBDFG*FGDG
ABC*654654*1*57.84*45.4*88*2*6546546545
CCC*WSF*SG*FGH*GHJ
ADA*AF*SFG*DFGH*FGH*FGTH

I need to select the line starting with "ABC" its 7th field should be replaced as 13
I result code should look like the one given below:
Code:
ABC*654654*1*54.54*21.2*87*13*654654654654
CCC*FS*FS*SFD*DSF
GGG*FGH*CGB*FBDFG*FGDG
ABC*654654*1*57.84*45.4*88*13*6546546545
CCC*WSF*SG*FGH*GHJ
ADA*AF*SFG*DFGH*FGH*FGTH

# 2  
Old 12-14-2011
Code:
awk 'BEGIN {FS="*"; OFS="*"} /^ABC/ {$7="13"; print}' filename

But you're never going to learn if we just give you the answers...
# 3  
Old 12-14-2011
That looks like a fine illustration of awk's basic principles actually Smilie I'll just comment it a bit more:

Code:
# awk code blocks work like CONDITION { code } WHENTOPRINT
# For each line, { code } gets run when CONDITION is true
# and the line gets printed back out when WHENTOPRINT is true.
# The code block doesn't have to be executed for WHENTOPRINT to
# cause it to print a line.
#
# CONDITION is default true if not given.  WHENTOPRINT is default false.

awk '
# Special code section run before any lines are read
BEGIN {
        # Field separator.  It defaults to whitespace but can be anything.
        # Lines read get split on it, put into special variables $1,$2,...
        # all the way up to NF, the last field.  NF is the number, $NF is the
        # last field itself.  $0 is the entire line.
        # ( $ is an operator, by the way.  You can do N=1; print $N to print field 1)
        FS="*"
        # When the 'print' command is used, awk puts the line back
        # together from the $1,$2,...$NF fields before printing it, you can
        # control what it joins together fields with.
        OFS="*"}

# The code block gets run whenever the regex /^ABC/ is true --
# i.e. whenever the line begins with ABC.
/^ABC/ {
        # You can set fields, not just read them, handy for altering text
        # On the go.  Here, we change the 7th field into 13 for any line
        # beginning with ABC.
        $7="13";
        # You can also tell awk when to print explicitly, by using the 'print'
        # command.  By default it prints out the entire line assembled from
        # the current contents of $1,$2,...$NF
        print }' filename


Last edited by Corona688; 12-14-2011 at 11:54 AM..
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 12-16-2011
OP wanted everyline to be printed right?
Code:
awk 'BEGIN{FS="*"; OFS="*"} /^ABC/{$7="13"}1' input_file

BTW, 1 here is WHENTOPRINT which is true and prints everything by default.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Read line by line and replace string.

Hi, I currently have a problem that I need to read a file line by line. After I read it line by line there are some commands in which I have to change a specific string.(In my case, I have to make a script that changes all the passwords into hash value) Here is a sample input... (3 Replies)
Discussion started by: thebennnn
3 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

6. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

9. Shell Programming and Scripting

Need to find a string, check the next line, and if it matches certain criteria, replace it with a s

Hey Fellas. I am new to scripting. I have searched through the forums and found a lot of good info, but I can't seem to get any of it to work together. I am trying to find a particular sting in a file, and if the next string matches certain criteria, replace it with a string from a csv... (6 Replies)
Discussion started by: midniteslice
6 Replies

10. Shell Programming and Scripting

search for a string ,replace the whole line with new line

hai i am very new to unix. i am having two files like this. first.properties cache.ZA.TL_CCY=SELECT trim(CCY_CODE)||trim(COUNTRY_CODE)||trim(CITY_CODE) AS... (4 Replies)
Discussion started by: kkraja
4 Replies
Login or Register to Ask a Question