how to concatenate the grep output and the indexed string in bash? - bash

I am running a for loop such like
for d in /dir1/dir2/*.txt:
do
cat $d | grep "^ABC" >output.txt
done
My question is how to concatenate both $d and the output for grep? such like
/dir1/dir2/demp1.txt ABCDEFG
/dir1/dir2/demp2.txt ABCD
...

Within your loop store the output of the command in a variable:
e=$(cat ${d} | grep "^ABC")
echo -e "$d\t$e" >> output.txt

Related

Bash count non-commented lines and write to output with filename

I would like to count the non-commented lines in multiple files and append the result to an output file
This is how I would count the non-commented lines for multiple files, but I don't know how to store the result together with the filename in an output.txt file.
for file in *txt
do
cat "$file" | sed '/^\s*#/d' | wc -l
done
You can write several things per line, and you can redirect the output of the whole loop to a file:
for file in *txt
do
echo -n $file' '
cat "$file" | sed '/^\s*#/d' | wc -l
done > output.txt
Also you can shorten the file processing down to:
egrep -v '^\s*#' "$file" | wc -l
for file in *txt
do
cat "$file" | sed '/^\s*#/d' | wc -l >> output.txt
done

Cannot assign output of sed to a variable

I refered this
https://unix.stackexchange.com/questions/251388/prefix-and-suffix-strings-to-each-output-line-from-command
to add prefix to output of ls.
I want to store this output of ls:
file1
file2
file3
in to a variable with value:
/../file1 /../file2 /../file3
This is my .sh file:
PREFIX="/../"
OUTPUT_INLINE=$(ls | tr "\n" " ")
OUTPUT="${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
echo "${OUTPUT_INLINE}"
echo "${OUTPUT}"
Output is:
file1 file2 file3
It means variable OUTPUT contains nothing.
Even if I do:
echo "${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
I will get:
/../file1 /../file2 /../file3
What is wrong here ?
You are assigning OUTPUT variable this command
"${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
Which means nothing.
Do as you are already doing with OUTPUT_INLINE variable to assign the output of command.
OUTPUT=$(echo -n "${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g")
OUTPUT="${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
That pipes OUTPUT="${OUTPUT_INLINE}" into sed "s|\<|$PREFIX|g", which doesn’t do anything. I think you meant:
OUTPUT=$(printf '%s' "${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g")
but there’s lots of fragility here around different delimiter types, and you should be able to avoid all that:
PREFIX="/../"
for filename in *; do
printf '%s%s ' "$PREFIX" "$filename"
done

Grep command returns nothing in shell script

When I try to extract rows that are matched string which are in another file.But the grep command returns nothing.
#!/bin/bash
input="export.txt"
file="filename.csv"
val=`head -n 1 $file`
echo $val>export.csv
cat export.txt | while read line
do
val=`echo $line | tr -d '\n'`
echo $val
valu=`grep $val $file`
echo $valu
done
You can simply do this :
grep -f list.txt input.txt
Which will extract all the lines from input which match any word from list.txt.
If for some reason you want to save each match, you can do it in a Bash array as :
IFS=$'\n' read -d '' -a values <<< "$( grep -f list.txt input.txt )"
And then you can print a certain match as :
echo "${values[1]}"
Regards!

how to use tail and grep and get file name that have the result [duplicate]

This question already has answers here:
tail multiple files and grep the output [closed]
(3 answers)
Closed 1 year ago.
I am using tail and grep to find something like 'Apple' on multiple files
i need to get the file name that found the result in
tail -F files_name grep --line-buffered -e 'Apple' -e 'Orange' while read ; do while :; do echo -en "\007"; sleep 1; done ; done
Look to me like you overthinked.
grep 'Apple\|Orange' file1 file2
Searches for the string Apple or Orange in both file1 and file2. grep will output the filename with : as separator. You can use grep -l 'Apple\|Orange' file1 file2 to print filenames only.
tail -f *txt | awk '/^==> / {a=substr($0, 5, length-8); next} {print a":"$0}' | grep -ni "Apple\|Orange"

count specific word in line in bash

i have variable such as "1,2,3,4"
i want to count of commas in this text in bash
any idea ?
thanks for help
This will do what you want:
echo "1,2,3" | tr -cd ',' | wc -c
Off the top of my head using pure bash:
var="1,2,3,4"
temp=${var//[^,]/}
echo ${#temp}
Isolate commas per line, count lines:
echo "$VAR"|grep -o ,|wc -l
very simply with awk
$ echo 1,2,3,4 | awk -F"," '{print NF-1}'
3
with just the shell
$ s="1,2,3,4"
$ IFS=","
$ set -- $s
$ echo $(($#-1))
3
A purely bash solution with no external programs:
$ X=1,2,3,4
$ count=$(( $(IFS=,; set -- $X; echo $#) - 1 ))
$ echo $count
3
$
Note: This destroys your positional parameters.
Another pure Bash solution:
var="bbb,1,2,3,4,a,b,qwerty,,,"
saveIFS="$IFS"
IFS=','
var=($var)x
IFS="$saveIFS"
echo $((${#var[#]} - 1))
will output "10" with the string shown.
echo '1,2,3' | grep -o ',' | wc -l

Resources