How to get line by line from file and cut string before # on the bash - bash

I have file and its content :
gnome-tweak-tool #(Cinnamon does not support)
gnome-system-monitor
gnome-calculator
gedit
nomacs gimp #(viewer and editor for image)
#aa
#bb
After merge with command of "pacman".
I desire output like :
pacman -S gnome-tweak-tool
pacman -S gnome-system-monitor
pacman -S gnome-calculator
pacman -S gedit
pacman -S nomacs gimp
Does anybody help me?
Thanks!

This awk one-liner will do the job:
awk -F'#.*' '$1&&$0="pacman -S "$1' file
Note that, pacman accepts multiple packages, like:
pacman -S package1 package2 package3...

Try this. This will output to stdout:
awk -F# '{print $1}' file | awk 'NF' | sed -e 's/^/pacman -S /'
This will output to a new file:
awk -F# '{print $1}' file | awk 'NF' | sed -e 's/^/pacman -S /' > file2

Try this
sed -e 's/#.*$//' test.in | awk '{if (NF != 0) {printf("pacman -S %s\n", $0)} else {print $0}}'
Hope this helps

First remove all the lines starts with #
sed 's/#.*//g;/^$/d' file1.txt > tmp.txt
Now concat pacman -S in front of each line
awk '{print "pacman -S " $0;}' tmp.txt > file1.txt
Hope this will help you

sed only:
sed -n 's/#.*//; /./ s/^/pacman -S /p' inputfile
Explanation:
sed -n Don't print unless specified
s/#.*// Remove everything from '#'
/./ ... /p Only print lines with at least on char left
s/^/pacman -S / Insert "pacman -S"at the start of the line

pacman -Qqen > pkglist.txt
To install:
pacman -S - < pkglist.txt
From ArchWiki: https://wiki.archlinux.org/index.php/Pacman/Tips_and_tricks#List_of_installed_packages

Related

Echo command containing both double and single quotes

I have this command
cp $(ldd MyApp.out | awk '{print $3}' | sed -E '/^$/d') lib/
and at some point, I want to echo it into a file but a naive approach echo command_above doesn't work.
If I put the command into single quotes, then $3 expands to whitespace.
Is it possible to print that command char-by-char as it is after echo command without any expansion and substitution?
The common approach is to use the << operator to read until some delimiter:
# "cat" just prints what it reads
cat << 'EOF' > output_file
cp $(ldd MyApp.out | awk '{print $3}' | sed -E '/^$/d') lib/
EOF
Use xargs to pass file names list to cp
ldd MyApp.out | awk '$3!=""{print $3}' | xargs -d'\n' -I{} cp {} lib/
For debugging and logging purposes you can use set -x or set -v:
set -v # dump commands below
cp $(ldd MyApp.out | awk '{print $3}' | sed -E '/^$/d') lib/
set +v # stop dumping

Count how many words in file test.txt start with “tol”?

I'm new to Linux shell. I know there are tools to do this thing, such as awk. But I'm wondering if I could do it using grep or wc or other commands? awk seems intimidating to me. Thanks.
I tried grep and wc, like this:
grep tol test.txt | wc -w
But grep will give me the whole line.
If I tried the following:
grep '^tol$*' test.txt | wc -w
It only counts the line begins with mol.
How can I grep the words starting with tol?
Something like that:
grep -o '\<tol[[:alpha:]]*\>' test.txt | wc -w
< - for beginning of the word,
> - the end of the word.
[[:alpha:]] - to avoid match of combinations like tol123 (You said you need only words).
-o - to show only matches, not the entire line.
You can do the same fairly simply with awk, e.g.
awk '{for(i=1;i<=NF;i++) $i~/^tol/ && n++} END {print n}'
Example
$ echo -e "tolerance topaz tolstoy\nbats toluene toledo" |
> awk '{for(i=1;i<=NF;i++) $i~/^tol/ && n++} END {print n}'
4
Another option is to translate all whitespace characters into linefeeds so that each word starts on a new line, then grep can count them itself:
echo -e "tolerance topaz\ttolstoy\nbats toluene toledo" | tr '[:space:]' '\n' | grep -c "^tol"
4
Or, if using a file called words.txt:
tr '[:space:]' '\n' < words.txt | grep -c "^tol"

Can't grep file correctly

I have a file with very simple syntax:
cat /tmp/test
ARCH=""prtconf -b | awk '/^name:/ {print $2}'
I tried to grep it:
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print $2"
ARCH=""prtconf -b | awk '/^name:/ {print $2}'
Let's make grep string a little longer, add } to the end:
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print $2"}
Nothing found
Why when I add } to the end of the line grep stop working?
OS is Solaris 10U11
$2 refers to command-line parameter so here it will substitute blank character in a patter. So you'l need to escape $ by slash like \$
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print \$2}"
Without adding } in your patter it was working because it was matching actual pattern as prtconf -b | awk '/^name:/ {print for your input. But if you add } in your patter then it will try to match prtconf -b | awk '/^name:/ {print } (which isn't there in your file so it won't show output.)

No output when using awk inside bash script

My bash script is:
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=echo"$output" | awk '{print $1}'
echo $score
The above script prints just a newline in my console whereas my required output is
$ curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*
?)<\/title>.*/\1/p' | awk '{print $1}'
SA
So, why am I not getting the output from my bash script whereas it works fine in terminal am I using echo"$output" in the wrong way.
#!/bin/bash
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=$( echo "$output" | awk '{ print $1 }' )
echo "$score"
Score variable was probably empty, since your syntax was wrong.

bash: grep only lines with certain criteria

I am trying to grep out the lines in a file where the third field matches certain criteria.
I tried using grep but had no luck in filtering out by a field in the file.
I have a file full of records like this:
12794357382;0;219;215
12795287063;0;220;215
12795432063;0;215;220
I need to grep only the lines where the third field is equal to 215 (in this case, only the third line)
Thanks a lot in advance for your help!
Put down the hammer.
$ awk -F ";" '$3 == 215 { print $0 }' <<< $'12794357382;0;219;215\n12795287063;0;220;215\n12795432063;0;215;220'
12795432063;0;215;220
grep:
grep -E "[^;]*;[^;]*;215;.*" yourFile
in this case, awk would be easier:
awk -F';' '$3==215' yourFile
A solution in pure bash for the pre-processing, still needing a grep:
while read line; do
OLF_IFS=$IFS; IFS=";"
line_array=( $line )
IFS=$OLD_IFS
test "${line_array[2]}" = 215 && echo "$line"
done < file | grep _your_pattern_
Simple egrep (=grep -E)
egrep ';215;[0-d][0-d][0-d]$' /path/to/file
or
egrep ';215;[[:digit:]]{3}$' /path/to/file
How about something like this:
cat your_file | while read line; do
if [ `echo "$line" | cut -d ";" -f 3` == "215" ]; then
# This is the line you want
fi
done
Here is the sed version to grep for lines where 3rd field is 215:
sed -n '/^[^;]*;[^;]*;215;/p' file.txt
Simplify your problem by putting the 3rd field at the beginning of the line:
cut -d ";" -f 3 file | paste -d ";" - file
then grep for the lines matching the 3rd field and remove the 3rd field at the beginning:
grep "^215;" | cut -d ";" -f 2-
and then you can grep for whatever you want. So the complete solution is:
cut -d ";" -f 3 file | paste -d ";" - file | grep "^215;" | cut -d ";" -f 2- | grep _your_pattern_
Advantage: Easy to understand; drawback: many processes.

Resources