Sponsored Content
Top Forums Shell Programming and Scripting Failure: if grep "$Var" "$line" inside while read line loop Post 303024307 by bakunin on Friday 5th of October 2018 05:49:35 PM
Old 10-05-2018
Quote:
Originally Posted by Subsonic66
Code:
"if grep -qv "$optl" "$line"

doesn't work --> Error messages: "grep: 0/0 RAID1 Optl: No such file or directory" and "grep: 1/1 RAID1 Optl: No such file or directory"

BUT

Code:
"if grep -qv "$optl" /opt/lsi/storcli/templine.txt

works as expected. But I would prefer to work with the $line-variable.
Sorry, but you set yourself up for that: it usually works as soon as you do it right. ;-))

Seriously: grep expects its arguments in the form (i simplify to the relevant here): grep <pattern> <file>. What you fed it instead, though, is: grep <pattern> <variable, that is: string which to search for pattern>. This is why grep treated the string you wanted to be searched as a filename - and of course this filename is not found, which is why you got the error of "file not found".

There is in fact another form of grep-invocation, where you can feed grep something from <stdin>, like this:

Code:
some_process | grep <pattern>

To get your string there is possible but you first have to create a process with the string as output. Can you guess which process that could be?

Yes, of course: echo "$variable". Therefore, here is your solution:

Code:
echo "$variable" | grep <pattern>

Picture grep as a machinery which has two different intakes: a file or <stdin>. Just like a coffee machine might have two intakes, one for ground coffee and one for whole beans. Which one is selected depends on how it is invoked. But you cannot pour your data into the wrong intake and then expect grep to do it right: put your beans into the opening for ground coffee and the coffee machine will make all sorts of things but probably not coffee.

I hope this helps.

bakunin

PS: CR/LF line endings are indeed important and will confuse a lot of UNIX utilities. I suggest to correct them to UNIX newlines first thing. There are unix2dos and dos2unix, two utilities which change back and forth from/to UNIX-style line breaks from/to DOS-style line breaks, sed-scripts and many other ways of achieving the same.

PPS: you do not need to create a temporary file at all in your code: as i have shown you you can directly work with the variable content.

Last edited by bakunin; 10-05-2018 at 06:59 PM..
This User Gave Thanks to bakunin For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

7. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

8. Shell Programming and Scripting

Bash - How to do a "read -p" inside a while loop?

Hi there guys! I was trying to do: while read line; do if ; then read -p "Press Enter to continue..." cont=0 fi echo $line let cont++ done < file.txt However, I have read that the read -p would not work in a while loop... I was wondering if there is any other way to... (2 Replies)
Discussion started by: rplae
2 Replies

9. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies
All times are GMT -4. The time now is 05:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy