cipherlogic
asked on
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
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
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 < filename > some_output_file
wmp
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 < filename > some_output_file
wmp
ASKER
I'll try that in a bit.
Please note that both solutions require using the bash shell!
ASKER
;)
The second solution is not very different from
sed -e "sed_subcommands" filename > some_output_file
sed -e "sed_subcommands" filename > some_output_file
ASKER
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
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
ASKER
Got it in a one liner.
while read line; do echo -e "$line"; done < names.txt | grep description ldap.ldiff
while read line; do echo -e "$line"; done < names.txt | grep description ldap.ldiff
ASKER
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.
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.
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
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
ASKER
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.
ASKER
It seems you're correct. I examined the output file and found it is just like running grep on the file.
ASKER
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.
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.
ASKER
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...
sed -n '/namevariable/{N;N;N;N;N;
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...
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 ...
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;
| grep -f - ldapdump > outputfile
Remains the thing with the blank lines ...
ASKER
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.
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.
while read namevariable
do
sed -n '/'$namevariable'/{N;N;N;N
done < names.txt
Please note the additional single quotes ('') and the "$" character in front of the variable name.
ASKER
well....except namevariable isn't any real variable. Its just a username per line...
ASKER
Hang on - let me try to hash out what you're saying. I think I may get it now.
... namevariable isn't any real variable ...
Yes it is, because of "while read namevariable"
Yes it is, because of "while read namevariable"
ASKER
#!/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.
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.
ASKER
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
#!/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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This did it...
.. except for the blank line, I guess.
.. except for the blank line, I guess.
ASKER
Its cool. This is enough to let me refine output. I've suggested to v close with you getting full points.
ASKER