awk replacement problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk replacement problem
# 1  
Old 08-14-2010
awk replacement problem

hi

i am using awk for first time so i am having issue

is it the correct way of using

Y here is variable which i fetch using grep from file

Code:
awk -v X={$Y}
{
if ($0 ~ /X/ ) { sub (out.*/,"out1",$0) }
print 0
}, filename > temp


when i look into temp file i dont see any replacement done

Last edited by Scott; 08-14-2010 at 12:12 PM.. Reason: Please use code tags
# 2  
Old 08-14-2010
Almost (assuming you want to replace everything from "out" to the end of the line with the word "out1").

Code:
awk -v X="$Y" '
{
  if ( $0 ~ X ) { sub (/out.*/,"out1",$0) }
  print $0
}' filename > temp

The same, but a bit more concise:

Code:
awk -v X="$Y" '
  $0 ~ X { sub (/out.*/,"out1",$0) }
  1
' filename > temp

# 3  
Old 08-14-2010
not working

doesn't seem to work .

the second option through errors

any suggestion plz
# 4  
Old 08-14-2010
Quote:
Originally Posted by xyzstar
doesn't seem to work .

the second option through errors

any suggestion plz
That's really helpful information. My crystal ball is on the blink, why don't you say what doesn't work?

And in case you are using Solaris, use /usr/xpg4/bin/awk, or nawk.
# 5  
Old 08-14-2010
cool seem to work

we need to remove extra // when using in awk for variable.

Although second approach still dont work

i am using multiple if

---------- Post updated at 10:35 AM ---------- Previous update was at 10:34 AM ----------

sorry scott new to forum thing .
will get used to it in some time
# 6  
Old 08-14-2010
Quote:
Originally Posted by xyzstar
we need to remove extra // when using in awk for variable.

Although second approach still dont work

i am using multiple if

---------- Post updated at 10:35 AM ---------- Previous update was at 10:34 AM ----------

sorry scott new to forum thing .
will get used to it in some time
If you are using "multiple ifs" then it's not the same code as you posted, or by any account the same question.

Why not post all the relevant code, and some sample input data?

It would also help if you actually stated what your intention was too.

Thanks.
# 7  
Old 08-14-2010
here it goes

Code:
awk -v X={$Y} -v M={$N} -v str={$K}
{
if [str='1'] then {
if ($0 ~ /X/ ) { sub (xyz.*/,"xyz",$0) }
if ($0 ~ /M/ ) { sub (xyz.*/,"xyz",$0) }
}
print 0
}, filename > temp

this is the full code.i want to replace using awk
only if str='1'

example of
X=abcd.xyz
Y=ecdf.xyz
str='1' or any other value

let me know if u need any other info

Last edited by Scott; 08-14-2010 at 12:46 PM.. Reason: Code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed or awk Replacement/Addition

Hi, I have a big text file with similar data as below and need the text as in output using awk or sed. any help is greatly appreciated. Input: City=Chicago Elden street >>> reservedBy = business 1 >>> reservedBy = business 2 >>> reservedBy = business 3 City=Dallas Elm street >>>... (5 Replies)
Discussion started by: tech_frk
5 Replies

2. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

3. Shell Programming and Scripting

Incomplete replacement/substitution awk

Hi! (I'm just a newbie in awk & bash scripts) I have a script which replaces one column from an input file with a specified one from the same file. The input and the desired output files are shown below. ~ cat input.file random text Fe 1.33 23.23 3.33 C 21.03 23.23 3.33 Cu 0.00 ... (2 Replies)
Discussion started by: radudownload
2 Replies

4. Shell Programming and Scripting

awk's gsub variable in replacement

I been trying to figure out how to use element of array as a replacement pattern. This works as I expected: $ echo "one two three" | awk '{ gsub(/wo/,"_BEG_&_END_",$2); print }' one t_BEG_wo_END_ three $ echo "one two three" | awk '{ tmp="foo"; gsub(/wo/,"_BEG_" tmp "_END_",$2);... (5 Replies)
Discussion started by: mirni
5 Replies

5. Shell Programming and Scripting

Awk replacement problem

Hello everyone, I have a problem with awk replacement... I need to replace "|\n" for "\n" I tried thisawk '{ sub(/\|\\n/, "\\n"); print }' but it seems like it doesn't work properly. Can anyone help with that? (4 Replies)
Discussion started by: 1tempus1
4 Replies

6. Shell Programming and Scripting

Awk replacement

Hi all, I need help in replacing awk with sed for the below. 1 ) cat list | awk -F" |," '/MATH/ {sub(/]*/,"",$3); print $3}' Eg: file : list Sno Subno Name 1 SUB1 ENG 2 SUB2 MATH 2) Eg:result Total No of Students: 2 Sno ID Sub ------------------ 1 ... (3 Replies)
Discussion started by: priyam
3 Replies

7. UNIX for Dummies Questions & Answers

awk pattern replacement

Hi I'm a newbie in unix and I'm having trouble in creating a script. I want to search for a pattern '_good' and insert new lines that contains '_bad', '_med', '_fail' while also ensure that the line contains _good is removed here some of the data UPDATE SCHOOL SET GRADE =... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

8. Shell Programming and Scripting

perl as awk replacement in a script.

Hey all, Im trying to write a script on windows, which Im not too familiar with. Im generally a bash scripting guy but am using perl for this case. My question is... I have this exact output: 2 Dir(s) 6,380,429,312 bytes free and I just need to get the number out... (4 Replies)
Discussion started by: trey85stang
4 Replies

9. Shell Programming and Scripting

AWK String replacement

I have an xml file with following tags <NewTag>value123</xyz> <NewTag>value321</abcd> I have to replace the values in between the tags with some value ( VAL1/VAL2) but the thing the ending tag can be any thing, for this i need a awk command currently i am using this but it... (5 Replies)
Discussion started by: subin_bala
5 Replies

10. Shell Programming and Scripting

Awk variable replacement

I have a function awkvarrep() { awk -F'|' '$1~/$1/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile } I'm calling it by this VARREP=XYZ awkvarrep $VARREP since i'm passing $VARREP to the awkvarrep() function I want to use this with $1 but it dosen't seem to be... (5 Replies)
Discussion started by: iAm4Free
5 Replies
Login or Register to Ask a Question