LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2012, 05:49 PM   #1
CrowdstarQA
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
Shell Script - Cant get sed to replace a line from a textfile


Hi all,

Below is a script to parse IOS crash logs. What I wanted to do, was take in the file process the command, then replace the lines that have our appname in them. Currently the output, looks identical to the original file. I think the sed line is the issue (woof=`echo $tempFile | sed `$cmd``). Any help would be greatly appreciated. Banging my head against the desk at the moment.



#! /bin/bash
APPNAME=$1
FILENAME=$2
BUGNAME=$3
echo APPNAME IS $APPNAME
echo FILENAME IS $FILENAME
contentsOfFile=`cat $FILENAME`
tempfile=$contentsOfFile
echo $tempfile
for i in `grep -n $APPNAME $FILENAME | grep [1-9]* | awk '{print $1 "," $2 "," $3 "," $4 "," $5 "," $6}'`
do
myline=$i
linenumber=`echo $myline | cut -d':' -f 1`
echo linenumber: $linenumber
hexaddr=`echo $myline | cut -d',' -f 3`
echo $hexaddr
if [[ -n "$hexaddr" ]]
then
atosResult=$(atos -arch armv7 -o $APPNAME".app/$APPNAME" $hexaddr)
else
hexaddr=0x00000
#atosResult=$(atos -arch armv7 -o $APPNAME".app/$APPNAME" $hexaddr)
fi
sedCommand=$linenumber's'
echo $sedCommand
echo Atos command before sed: $atosResult
atosResult=`echo $atosResult | sed "s/'//"`
echo Atos command after sed: $atosResult
cmd="$sedCommand/.*/$atosResult/"
echo CMD:$cmd
woof=`echo $tempFile | sed `$cmd``
echo Woof: $woof
done
for f in "${tempfile[@]}"
do
echo "Processing $f file..."
echo >>"$OUTPUT"

while IFS='' read -r line; do
printf "%s\n" "$line">>"$OUTPUT"
done <"$f"
echo >>"$OUTPUT"
echo >>"$OUTPUT"
done
echo "$tempfile" > $BUGNAME


Edited: It seems to be a problem with the atos command and what it returns. This has [ brackets - dashes and * in the text. See my post below for an example. I need a way to in line of text replace a line with the atos result. Thanks all.

Last edited by CrowdstarQA; 02-23-2012 at 12:00 PM.
 
Old 02-22-2012, 06:05 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
That said line that you pointed out is most likely not being parsed properly, due to the back ticks. I would try to set a new variable as the output of $cmd, and then set "woof" as $(echo $tempFile | sed $cmd_var) or something of that nature. Also, try to use $() instead of backticks, as using backticks is older practice.
 
Old 02-22-2012, 11:45 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Try also adding set -xv to the start of the script (ie after shebang) and view what is happening as each line is processed.
 
Old 02-23-2012, 11:22 AM   #4
CrowdstarQA
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
More Help -Pls

Hi All,

I think it has to do with the output of the atos command. It has several spaces and crazy characters. For instance
'+[OFASIHTTPRequest runRequests] (in ourgame) (OFASIHTTPRequest.m:4501)' When sed runs against this it either gives invalid command code or runs out of
"181s/.+/+[OFASIHTTPRequest": unterminated substitute in regular expression <---that warning. How can I pass the sed to have it ignore those characters? Keep in mind every line will be dynamic in how it comes from atos and each line of code. Thanks all in advance.

CrowdQA
 
Old 02-23-2012, 11:53 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Try to put $cmd into double quotes (instead of backticks), like this
Code:
woof=`echo $tempFile | sed "$cmd"`
Also make sure $atosResult does not contains slashes.

Last edited by firstfire; 02-23-2012 at 11:55 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script replace a line from a file dthanoon Programming 11 03-05-2010 09:46 AM
How to replace line with another line in shell script pramod.srk Linux - Newbie 5 02-26-2010 06:00 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
How to replace space using sed in shell script Ashok_mittal Linux - Newbie 2 09-08-2008 07:05 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration