Visit Our UNIX and Linux User Community


problem in greping the string from file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in greping the string from file using awk
# 1  
Old 04-23-2008
problem in greping the string from file using awk

I am using awk command for greping an value from the file
the file contains ..

file
----------------------------
content-----------
--------
String main = "81507066666";
------------------------------

i am greping the above value using awk command

NumberToReplace=`cat "file" | grep "String main"|awk -F '"' '{ print $2}'`
StringToReplace="String main = \"$NumberToReplace\""

when I echo the variable $StringToReplace ..the empty space is coming

echo $StringToReplace

out put
StringToReplace=String main = " 819066540205"

i want the output like

StringToReplace=String main = "819066540205"

pls help me out from this problem..waiting for the reply

thanks
vastare
# 2  
Old 04-23-2008
I cannot reproduce the problem with the code you posted. The problem sounds like you have a space in your commands but you forgot to copy+paste precisely that space here ...?

Code:
vnix$ cat file
String main = "81507066666";
vnix$ NumberToReplace=`cat "file" | grep "String main"|awk -F '"' '{ print $2}'`
vnix$ StringToReplace="String main = \"$NumberToReplace\""
vnix$ echo "$StringToReplace"
String main = "81507066666"

Incidentally, cat | grep | awk is a magnificent Useless Use of Cat. Google for that. Here's a slightly less useless way to say that:

Code:
NumberToReplace=`awk -F '"' '/String main/ { print $2 }' "file"`

Anyway, if you want to replace that line in that file, you should probably be doing it all in a single awk command.

Code:
awk -F '"' -v newvalue="1234567" 'BEGIN { OFS=FS } /String main/ { $2 = newvalue; } { print }' "file"


Previous Thread | Next Thread
Test Your Knowledge in Computers #872
Difficulty: Medium
In computer science, self-modifying code is code that alters its own instructions while it is executing.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. UNIX for Beginners Questions & Answers

Qn on awk greping values

Hello Experts, I was trying to awk out some data out of a text file. Below is a sample file which I have xxx ***Wed Jun 28 18:00:59 CDT 2015 avg-cpu: %user %nice %system %iowait %steal %idle 17.10 0.00 4.56 2.86 0.00 75.48 Device: rrqm/s wrqm/s ... (2 Replies)
Discussion started by: DevAnand
2 Replies

3. Shell Programming and Scripting

awk and greping between lines

i want to grab lines from a file that are between two patterns (including the lines that contain the pattern). here's what im currently doing: data.txt aaa bbb cccc ddd eee ffff ddd code: awk '/bbb/,/fff/ && $0 !~ /ddd/' cdsnmp.sh I want to grab lines between and including bbb... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Greping the string from a log

Hi Folks, I am searching something in multiple logs , actually an even ocuurs which get recorded in logs with a special word so rite now I am opening an individual log in vi and then searching it but total in the directory there are such 15 logs and it is very difficult to go one by one so... (1 Reply)
Discussion started by: punpun66
1 Replies

5. Shell Programming and Scripting

Greping the column with a value greater than 50 with awk

Hi All, I am running a command from a remote server using ssh to different servers. I will get a output like below with 4 columns. I want to grab line which is having a coulmn which grate than or equal to 50. How can I do it with Awk or sed ??. I add a space to first row with sed 's/::/:: /g' to... (4 Replies)
Discussion started by: raghin
4 Replies

6. Shell Programming and Scripting

greping last occurrence of the searched string

Hello, I have active log file i want to grep the last occurrence of the word in that log file the log file gets on increasing and increasing i want to fetch it from live file. Please guide me, Thanks in advance (4 Replies)
Discussion started by: vidurmittal
4 Replies

7. Shell Programming and Scripting

greping date in a file

i have a script that checks inside the file and find the start date and end date with time........... #!/bin/ksh cd /ednadtu3/u01/pipe/logs TZ=`date +%Z`+24 ;b=`date +%Y-%m-%d` echo $b for i in DBMaint.log do echo "Start Time:" >> /ednadtu3/u01/pipe/naveed/Report.txt cat $i | grep... (3 Replies)
Discussion started by: ali560045
3 Replies

8. Shell Programming and Scripting

Greping a string for only one occurence

Hi, I have a csv ( comma seperated value) file and I want to search for a particular string in each line of that file only if it occurs only once in the line. That is same string may be present more than once in a line but I want it to be greped only when it occurs just once. Please advice... (17 Replies)
Discussion started by: Piscian
17 Replies

9. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies

10. UNIX for Dummies Questions & Answers

sed/awk String problem

I would appreciate it if any one can guide me in using awk perhaps sed in extracting some values from a long string. here is an example. .......some lines here........ ........ aaaa bbbb cccc ddddd eeeee fffff gggg (time:hhhh)........ ......some lines here also.......... How can I extract... (2 Replies)
Discussion started by: odogbolu98
2 Replies

Featured Tech Videos