I'm running the command below:
for line in $(cat file1.txt); do cat file2.txt | grep ${line}; done
and it´s works fine..but I'm looking a way to print the results in a file like this:
$(line) - grep result
Is there a way to customize my grep output?
Thanks
file1="file1.txt";
file2="file2.txt";
for line in $file1; do
result=$(cat $file2 | grep $line | sed -e :a -e N -e '$!ba' -e 's/\n/ /g'));
echo "$line-$result";
done
if the result of $(cat file2.txt | grep $line) are multi lines, you should implement to echo with for statement.
for line in $(file1.txt); do
result=$(cat file2.txt | grep $line);
echo $line
for _line in $result; do
echo " -$_line";
done
done
Related
In linux, how to create a file with content whose single line with \n (or any line separator) is translated into multi-line.
fileA.txt:
trans_fileA::abcd\ndfghc\n091873\nhhjj
trans_fileB::a11d\n11hc\n73345
Code:
while read line; do
file_name=`echo $line | awk -F'::' '{print $1}' `
file_content=`echo $line | awk -F'::' '{print $2}' `
echo $file_name
echo $(eval echo ${file_content})
echo $(eval echo ${file_content}) > fileA.txt
The trans_fileA should be:
abcd
dfghc
091873
hhjj
You can do it this way (with bash):
# read input file line by line, without interpreting \n
while read -r line
do
# extract name
name=$(echo $line | cut -d: -f 1)
# extract data
data=$(echo $line | cut -d: -f 3)
# ask sed to replace \n with linefeed and store result in name
echo $data | sed 's/\\n/\n/g' > "$name"
# read data from given file
done < fileA.txt
You can even write a smaller code:
while read -r line
do echo $line | cut -d: -f 3 | sed 's/\\n/\n/g' > "$(echo $line | cut -d: -f 1) "
done < fileA.txt
I'm monitoring from an actively written to file:
My current solution is:
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | grep -q -e "enterpriseID:"
if [ $? = 0 ]
then
((ws_trans++))
fi
echo $LINE | grep -q -e "sc_ID:"
if [ $? = 0 ]
then
((sc_trans++))
fi
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
However when attempting to do this with AWK I don't get the output - the $ws_trans and $sc_trans remains 0
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | awk '/enterpriseID:/ {++ws_trans} END {print | ws_trans}'
echo $LINE | awk '/sc_ID:/ {++sc_trans} END {print | sc_trans}'
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
Attempting to do this to reduce load. I understand that AWK doesn't deal with bash variables, and it can get quite confusing, but the only reference I found is a non tail application of AWK.
How can I assign the AWK Variable to the bash ws_trans and sc_trans? Is there a better solution? (There are other search terms being monitored.)
You need to pass the variables using the option -v, for example:
$ var=0
$ printf %d\\n {1..10} | awk -v awk_var=${var} '{++awk_var} {print awk_var}'
To set the variable "back" you could use declare, for example:
$ declare $(printf %d\\n {1..10} | awk -v awk_var=${var} '{++awk_var} END {print "var=" awk_var}')
$ echo $var
$ 10
Your script could be rewritten like this:
ws_trans=0
sc_trans=0
tail -F /var/log/system.log |
while read LINE
do
declare $(echo $LINE | awk -v ws=${ws_trans} '/enterpriseID:/ {++ws} END {print "ws_trans="ws}')
declare $(echo $LINE | awk -v sc=${sc_trans} '/sc_ID:/ {++sc} END {print "sc_trans="sc}')
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
Below is my code and I am able to get the ssh connection. But after that it is doing nothing.
log_time="date +%F\%T"
PR_ONE="username#hostname"
file="/home/log.txt"
to_list="myemail"
echo "`$log_time`" >> $file
`ssh $PR_ONE "echo df -hP | grep fs1 | awk '{print $4}'"` >> $file
cat $file | mailx -s "Disk space usages" $to_list
I am getting the email but only with $log_time. I know that I am doing something wrong in quotations in the ssh line.
I am new to shell scripting.
Try this :
echo "df -hP | grep fs1 | awk '{print $4}'" | ssh -tt $PR_ONE >> $file
or prefer this :
ssh -tt $PR_ONE << EOF >> $file
df -hP | grep fs1 | awk '{print $4}'
EOF
I am trying to transform below 2 statements into a shell script
cat all5.log | grep 'Opened\|Closed'> all.log
awk -F ' ' '{print }' all.log | sort | uniq > uniqueFiles.txt
Here is monitorFd.sh bash script
#!/bin/bash
if [ "" != "" ]; then
cat | grep 'Opened\|Closed' > temp.log
awk -F ' ' '{print }' temp.log | sort | uniq > uniqueFiles.txt
while IFS='' read -r line || [[ -n "$line" ]]; do
cmd1=`cat | grep Opened | grep $line | sort | wc -l`
cmd2=`cat | grep Closed | grep $line | sort | wc -l`
echo 'Opened: '$cmd1', Closed: '$cmd2' '$line
done < "uniqueFiles.txt"
rm -f temp.log
else
echo "No target file provided. (hint: trace dump of file-leak-detector.jar)" #syntax error: unexpected end of file
In notepad++ I changed this file to be of UNIX format. Also changed permission to +x, but I am getting below exception.
monitorFd.sh: line 16: syntax error: unexpected end of file
What is wrong with this program?
The Syntax for if in bash is if ... else/elif ... fi. So just add a fi at the end of your file.
how to retrive every portion separately from following file name? DSA4020_frontcover_20346501_2011-05.doc
I want to retrieve informations as below;
name = DSA4020
type = frontcover
id = 20346501
date = 2011-05
is it possible to do with sed??
Yes, you can:
pax$ echo 'DSA4020_frontcover_20346501_2011-05.doc' | sed
-e 's/^/name=/'
-e 's/_/\ntype=/'
-e 's/_/\nid=/'
-e 's/_/\ndate=/'
-e 's/\..*//'
name=DSA4020
type=frontcover
id=20346501
date=2011-05
That's all on one line, I've just split it for readability.
You could also do it with awk if you wish:
pax$ echo 'DSA4020_frontcover_20346501_2011-05.doc'
| awk -F_ '{print "name="$1"\ntype="$2"\nid="$3"\ndate="substr($4,1,7)}'
name=DSA4020
type=frontcover
id=20346501
date=2011-05
awk may be a better choice
# f=DSA4020_frontcover_20346501_2011-05.doc
# name=$(echo $f | awk -F_ '{print $1}')
# echo $name
DSA4020
# type=$(echo $f | awk -F_ '{print $2}')
# echo $type
frontcover
In pure bash
FILE="DSA4020_frontcover_20346501_2011-05.doc"
eval $(echo $FILE |(IFS="_";read a b c d; echo "name=$a;type=$b;id=$c;date=${d%.doc}"))
echo Name:$name Type:$type ID:$id DATE:$date