• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Simple BASH help. Input from a file.

Hello,
I have a simple question. I'm trying to write a one liner or script to search a file for a name on each line. Then take that line as a variable and run that variable in this sed command I've come up with.
How do I read each line from the name file formatted 1 name per line and take it as a variable in the sed command and sent to some output file?
Thanks
0
cipherlogic
Asked:
cipherlogic
  • 17
  • 9
1 Solution
 
cipherlogicAuthor Commented:
Note, I tried where line read to try sending the line to the sed command as a variable but haven't succeeded yet.
0
 
woolmilkporcCommented:
This is for searching a file for a particular "search_string":

grep "search_string" filename | while read line
  do
    sed -e "sed_subcommands" <<< $line
  done > some_output_file

This is for processing every line:

while read line
  do
     sed -e "sed_subcommands" <<< $line
  done < filenamesome_output_file

wmp
0
 
cipherlogicAuthor Commented:
I'll try that in a bit.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
woolmilkporcCommented:
Please note that both solutions require using the bash shell!
0
 
cipherlogicAuthor Commented:
;)
0
 
woolmilkporcCommented:
The second solution is not very different from

sed -e "sed_subcommands" filenamesome_output_file
0
 
cipherlogicAuthor Commented:
Ok here's what I got. I still don't understand how to run this against the name file I created though...
The input file I want to run this against is names.txt

#!/bin/bash
 while read line;
 do
 sed -e -n '/$line/{N;N;N;N;N;p}' ldap.ldif | grep description;
 done > names.out
0
 
cipherlogicAuthor Commented:
Got it in a one liner.
while read line; do echo -e "$line"; done < names.txt | grep description ldap.ldiff
0
 
cipherlogicAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for cipherlogic's comment #a40304429
Assisted answer: 250 points for woolmilkporc's comment #a40304298

for the following reason:

My solution was best.
0
 
woolmilkporcCommented:
Sorry, I cant understand what your one-liner should be good for.

while read line; do echo -e "$line"; done < names.txt

is nothing else than

cat names .txt

Further, if you give grep the name of a file  to search in it will ignore stdin. So your code is equivalent to

grep description ldap.ldiff
0
 
cipherlogicAuthor Commented:
No, this code runs sed against every line in an input file and outputs to a file with grep being performed only on each line.
0
 
cipherlogicAuthor Commented:
It seems you're correct. I examined the output file and found it is just like running grep on the file.
0
 
cipherlogicAuthor Commented:
I basically have 3 files. 1 is a master list of users. 2 is the ldap dump. 3 is the output file.

I need this script to:
 take in the input file
 print each line in a file with the results of searching it as the variable in the sed command
   if it isn't found in the ldap dump, then print a blank line instead of results

That's about it.
0
 
cipherlogicAuthor Commented:
Basically take this:
sed -n '/namevariable/{N;N;N;N;N;p}' ldap.ldif | grep description

and run it each time a new line is encountered from the input file...

while read line; do echo -e "$line"; done < names.txt

...

bonus if I can print the username line from the input with blank lines if its not found with the sed...
0
 
woolmilkporcCommented:
print each line in a file with the results of searching it as the variable in the sed command

That's the same as

sed 'command' inputfile

sed works by default against each line of its input file. No need for a "line" variable.

You can take the results of the above (maybe once again filtered by grep) as a pattern list for grep:

sed -n '/namevariable/{N;N;N;N;N;p}' ldap.ldif | grep description
| grep -f - ldapdump > outputfile

Remains the thing with the blank lines ...
0
 
cipherlogicAuthor Commented:
Yes but I'm getting the namevariable from names.txt as the input file. I don't know how to pull the variable from the input file. Otherwise I have to run this sed by hand per user.
0
 
woolmilkporcCommented:
You want to read a file and use the strings found there in sed?

while read namevariable
 do
   sed -n '/'$namevariable'/{N;N;N;N;N;p}' ldap.ldif | grep description
 done < names.txt

Please note the additional single quotes ('') and the "$" character in front of the variable name.
0
 
cipherlogicAuthor Commented:
well....except namevariable isn't any real variable. Its just a username per line...
0
 
cipherlogicAuthor Commented:
Hang on - let me try to hash out what you're saying. I think I may get it now.
0
 
woolmilkporcCommented:
... namevariable isn't any real variable ...

Yes it is, because of "while read namevariable"
0
 
cipherlogicAuthor Commented:
#!/bin/bash
 while read line;
 do
 sed -n '/'$line'/{N;N;N;N;N;p}' ldap.ldif | grep description;
 done < names.txt >> names.out


This did the trick, except for it didn't print the usernames and if it wasn't present in the comparison file, didn't produce a blank line.

I need it to print the username above what it greps to show this username / username associated info below...and if its not there, I just want to see the username and a blank...Very close.
0
 
cipherlogicAuthor Commented:
This did it...

#!/bin/bash
 while read line;
 do
 echo $line;
 sed -n '/'$line'/{N;N;N;N;N;p}' ldap.ldif | grep description;
 done < names.txt >> names.out
0
 
cipherlogicAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for cipherlogic's comment #a40304611
Assisted answer: 500 points for woolmilkporc's comment #a40304534

for the following reason:

Most detailed for others to reference.
0
 
woolmilkporcCommented:
This will require a bit more code.

#!/bin/bash
 while read line
   do
     result=$(sed -n '/'$line'/{N;N;N;N;N;p}' ldap.ldif | grep description)
      if [[ ! -z "$result" ]]
         then
           echo $line
            echo $result
              else
                  echo $line
                  echo
       fi
    done < names.txt > names.out

I can't test the above right now, so there might be typos/syntax errors. If so, please let me know, I'll try to correct them.
0
 
woolmilkporcCommented:
This did it...

.. except for the blank line, I guess.
0
 
cipherlogicAuthor Commented:
Its cool. This is enough to let me refine output. I've suggested to v close with you getting full points.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 17
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now