grep for a backslash as for loop parameter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep for a backslash as for loop parameter
# 1  
Old 07-06-2011
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
Code:
 cat $file | /usr/xpg4/bin/grep -e $keyword -e \\\\

command works when its by itself, just not with the tick marks " ` " around it.

Does anyone know how to grep for a backslash and use the output in a for loop?

I tried google, but I haven't found anything that shows how to use the \\\\ escape character inside the tick marks.

Last edited by idlechatter; 07-06-2011 at 01:07 PM.. Reason: changed title
# 2  
Old 07-09-2011
Please post some sample input and the expected output from that input.

The following command will display lines matching loginstring followed by a space and a backslash:

Code:
grep 'loginstring \\' infile

# 3  
Old 07-11-2011
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass function parameter to do loop?

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)
Discussion started by: mysocks
5 Replies

2. UNIX for Dummies Questions & Answers

Help on for loop with a parameter

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)
Discussion started by: AAHinka
5 Replies

3. Shell Programming and Scripting

Loop over awk or passing parameter

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)
Discussion started by: fat
8 Replies

4. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

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)
Discussion started by: briandanielz
7 Replies

5. UNIX for Advanced & Expert Users

grep for a backslash question

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)
Discussion started by: cokedude
7 Replies

6. UNIX for Dummies Questions & Answers

Substituiton for grep paragraph parameter

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)
Discussion started by: glev2005
3 Replies

7. Shell Programming and Scripting

Problem if parameter has space in it using loop

for cmdopts in $*;do case $cmdopts in -mode) mode="$2";shift 2 ;; -server) server="$2";shift 2 ;; -Id) Id="$2";shift 2 ;; -passwd) passwd="$2";shift 2 ;; -rmtDir) rmtDir="$2";shift 2 ;; -lcDir) ... (9 Replies)
Discussion started by: pinnacle
9 Replies

8. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

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)
Discussion started by: jeriryan87
2 Replies

9. UNIX for Advanced & Expert Users

How to handle backslash in grep string

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)
Discussion started by: rajbal
6 Replies

10. Shell Programming and Scripting

How ro handle backslash character in grep?

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)
Discussion started by: rajbal
1 Replies
Login or Register to Ask a Question