How to grep for a backslash in a for loop statement
Hello everyone,
My main objective is to search for text within a file, namely a block of text where each line ends with a backslash "\".
However, the block must begin with a keyword, like "loginstring".
Here is an example of a file that contains a block:
Code:
###############
loginstring \
morestufff \
text \
no more text \n
###############
I would like to run a for loop through the file, line by line, and if i come across the keyword, see if the line ends in a backslash
here is the code I have now.
Code:
for line in `cat $file | /usr/xpg4/bin/grep -e $keyword -e \\\\`;do
if [ -n `echo $line | /usr/xpg4/bin/grep $keyword` ]; then
isBlock=1
fi
if [ ${line: -1} = "\\" ]; then
echo $line
fi
done
However, when i run the code above on the file, I get an error
"grep: RE error in \ trailing \ in pattern"
which I think has to do with the for loop syntax, since the
I'm trying to read the /usr/dt/config/C/Xresources file, specifically, the greeting.
The file looks like this
Code:
!!#########################################################
!!
!! Xresources
!!
!! Configuration file for the Login Manager
!!
...
...
Dtlogin*greeting.labelString: Hello! You are on %LocalHost% \n\n \
This computer is being monitored for all purposes. You will be responsible for \n \
all actions taken while logged in to this computer. Please make sure to \n \
Log out when you are finished with your session \n \
Have fun! \n
!! Dtlogin*greeting.persLabelString: Welcome %s
!! Dtlogin*greeting.alignmnet: ALIGNMENT_CENTER
!! ########################################################
!!
!! Size of Text Input Area
I just want to print out the "Dtlogin*greeting.labelString: " line and what it is set to.
So the output would be:
Code:
Dtlogin*greeting.labelString: Hello! You are on %LocalHost% \n\n \
This computer is being monitored for all purposes. You will be responsible for \n \
all actions taken while logged in to this computer. Please make sure to \n \
Log out when you are finished with your session \n \
Have fun! \n
I did find a workaround to that code i posted above. Instead of doing the grep inside the for loop, you grep and redirect output to a file, and loop over that file.
Here is the workaround:
Code:
file=/etc/dt/config/C/Xresources
tempfile=tmpfile.txt
keyword="Dtlogin\*greeting\.labelString:"
cat $file | /usr/xpg4/bin/grep -e "$keyword" -e \\\\ > $tempfile
for line in `cat $tempfile`; do
echo $line
done
rm $tempfile
Hi All,
I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter.
Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Hi,
need help to pass an paremeter to for loop script
$cat tp.ksh
for i in `grep $1 | cut -d "/" -f 5 | cut -d" " -f2`
do
fgrep $i $1 | grep 'with value' | cut -d "|" -f 2
done
$tp.ksh test_data.plan_49989_2015-05-01-00-13-38.log
command doesnot return the values. (5 Replies)
I wrote this script which works well when I manually input 55518622 and 1
but I need this script to be generic and loop over the following table
awk '$4>(55518622-500000) && $4<(55518622+500000)' chr1_GEN2bim | awk 'BEGIN {min=1000000000; max=0;}; {\
if($4<min && $4 != "") min = $4; if($4>max... (8 Replies)
I want to pass this array as a parameter.
IFS=$'\n'
fortune_lines=($(fortune | fold -w 30 ))
Inside of this line
screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`"
And I am lost at this point.
I am thinking something like this?
Then make it loop..
... (7 Replies)
Why does this work when grepping for a backslash?
grep '\\' .bash_history
grep "" .bash_historyWhy does this not work when grepping for a backslash?
grep "\\" .bash_historyI know this works works but just don't understand why I need 4 backslashes when using double quotes.
grep "\\\\"... (7 Replies)
My bash doesn't have grep -p.. I often find myself wanting to search a section of a man page for areas dealing with a specific property or parameter... For instance wanting to read everything in the man pages for bash that deal with Parameter Expansion..
Is there a way to do this with grep if... (3 Replies)
Say I have (in psuedocode)
For i=1 to 10
tar cvfb /... 5*i /junk(i)
end
What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Hi ,
I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it.
Diffu.txt
========
1159c1159
< <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466">
---
> <td... (6 Replies)
Hi ,
I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it.
Diffu.txt
========
1159c1159
< <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466">
---
> <td... (1 Reply)