Hi
i am trying to write a script to match a '\' at the END of the line.
For eg. i have a file Temp1 which contains :-

a = b + c \
+d;
printf("\n");

Objective:
To match the '\' in the 1st line and NOT the 3rd line (line with the printf)

When i try

grep '\\' Temp1

i am shown the 1st line and the 3rd line. However, i don't want the 3rd line.

Similarly, when i try

grep '\\$' Temp1

it does not work.

Plz help !

Recommended Answers

All 8 Replies

grep '\\$' Temp1

it does not work.


It works for me.

Did you create the file on a Windows box? If so, there will be a carriage return before the newline, and the backslash will therefore not be the last character on the line.

Thanks for the input. I am using a win ox (cygwin) . What could be the possible workaround ?

a = b + c \
+d;
printf("\n");

Technically those are all three "lines" but you're using the "\" to have the script interpret them as though they are one line. What you could do is match all of the \'s, then exclude all of the lines with \$ since they are not what you're looking for in this case.

sk@sk:~$ cat >> Temp1 << _EOF_
> a = b + c \
> +d;
> printf("\n");
> _EOF_
sk@sk:~$ grep '\\' Temp1 | grep -v '\\$'
printf("\n");
sk@sk:~$

Thanks for the input. I am using a win ox (cygwin) . What could be the possible workaround ?


Search for what is actually there: a backslash followed by a carriage return.

a = b + c \
+d;
printf("\n");

Technically those are all three "lines" but you're using the "\" to have the script interpret them as though they are one line.


They are three lines because the backslash is not the last character on the line; that's a carriage return.

If the backslash were the last character on the line, grep would still read it as three lines. Only the shell will interpret the backslash as a continuation character, and it would read the file as two lines.

how about converting Win-style end-of-line characters to UNIX system style end-of-line characters.

dos2unix input.txt
or
fromdos input.txt

Hi
i am trying to write a script to match a '\' at the END of the line.
For eg. i have a file Temp1 which contains :-

a = b + c \
+d;
printf("\n");

Objective:
To match the '\' in the 1st line and NOT the 3rd line (line with the printf)

When i try

grep '\\' Temp1

i am shown the 1st line and the 3rd line. However, i don't want the 3rd line.

Similarly, when i try

grep '\\$' Temp1

it does not work.

Plz help !

echo "hello testing world\\\\\nhello testing world"

echo "hello testing world\\\\\nhello testing world" | egrep -i "\\\\$"

Works.

--av.-

grep '\\.$' Temp1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.