This question already has answers here:
Remove empty lines in a text file via grep
(11 answers)
Closed 4 years ago.
Exists way to remove empty lines with cat myfile | grep -w #something ?
I looking for simple way for remove empty lines from my output like in the way the presented above.
This really belongs on the codegolfing stackexchange because it's not related to how anyone would ever write a script. However, you can do it like this:
cat myfile | grep -w '.*..*'
It's equivalent to the more canonical grep ., but adds explicit .*s on either side so that it will always match the complete line, thereby satisfying the word boundary conditions imposed by -w
You can pipe your output to awk to easily remove empty lines
cat myfile | grep -w #something | awk NF
EDIT: so... you just want cat myfile | awk NF?
if you have to use grep, you can do grep myfile -v '^[[:blank:]]*$'
Related
This question already has an answer here:
Using regex in Grep for Windows command line
(1 answer)
Closed 2 years ago.
Trying to find all occurrences of a word in a range of different .txt files in a directory.
I'm looking for commands that will give me an exact count if possible.
so far I have tried:
$ grep -w 'string' *
and:
$ grep --include=\*.{txt} -rnw desktop/testfiles/ -e "string"
The first outputs its entire contents and the second doesn't seem to do anything.
Any ideas?
simply :
grep -w "string" *.txt | wc -l
also GNU awk allows it to be done in single command
awk -v w="string" '$1==w{n++} END{print n}' RS=' |\n' *.txt
This question already has answers here:
Extract number from a line with awk/sed
(2 answers)
Closed 7 years ago.
I have a cookie file,I just want to extract the number before the .jpg extension using grep command ..how can i do it
USERSTAFFPHOTO 09480106177557.jpg
I want to copy 09480106177557
echo "USERSTAFFPHOTO 09480106177557.jpg" | grep -oE '[0-9]{1,}'
You should chain 2 grep commands, the first one to get the file name along with the .jpg extension and the second one to get only the number from the file name:
grep -oE '[0-9]+\.jpg' | grep -oE '[0-9]+'
Otherwise, you would get all the numbers that exist in the file with or without the .jpg extension.
Why restrict yourself to a particular tool? Do you want the number, or do you want to use grep? If the format is fixed, I would do this with tr:
echo USERSTAFFPHOTO 09480106177557.jpg | tr -cd [0-9]
If this is one line in a file and you need to filter it out, try awk:
awk '/USERSTAFFPHOTO/{print $2}' FS=' |\\.' input
This question already has answers here:
How can I extract a predetermined range of lines from a text file on Unix?
(28 answers)
Closed 7 years ago.
This seems pretty silly, but I haven't found a tool that does this, so I figured I'd ask just to make sure one doesn't exist before trying to code it up myself:
Is there any easy way to cat or less specific lines of a file?
I'm hoping for behavior like this:
# -s == start && -f == finish
# we want to print lines 5 - 10 of file.txt
cat -s 5 -f 10 file.txt
Even something a little simpler would be appreciated, but I just don't know of any tool that does this:
# print from line 10 to the end of the file
cat -s 10 file.txt
I'm thinking that both of these functionalities could be easily created with a mixture of head, tail, and wc -l, but maybe there are builtins that do this of which I'm unaware?
yes awk and sed can help
for lines 5 to 10
awk 'NR>4&&NR<11' file.txt
sed -n '5,10p' file.txt
for lines 10 to last line
awk 'NR>9' file.txt
sed -n '10,$p' file.txt
This question already has answers here:
Remove duplicate entries in a Bash script [duplicate]
(4 answers)
Closed 8 years ago.
Hi I am writing a script in bash which read the contents of files that have the word "contact"(in the current directory) in them and sorts all the data in those files in alphabetical order
and writes them to a file called "out.txt". I was wondering if there was any way in which I could get rid of duplicate content. Any help would be appreciated
The code I have written so far.
#!/bin/bash
cat $(ls | grep contact) > out.txt
sort out.txt -o out.txt
sort has option -u (long option: --unique) to output only unique lines:
sort -u out.txt -o out.txt
EDIT: (Thanks to tripleee)
Your script, at present, contains problems of parsing ls output,
This is a better substitute for what you are trying to do:
sort -u *contact* >out.txt
Use this using the uniq command (easier to remember than flags)
#!/bin/bash
cat $(ls | grep contact) | sort | uniq > out.txt
or the -u flag for sort like this
#!/bin/bash
cat $(ls | grep contact) | sort -u > out.txt
uniq may do what you need. It copies lines from input to output, omitting a line if it was the line it just output.
Take a look at the "uniq" command, and pipe it through there after sorting.
This question already has answers here:
Get string after character [duplicate]
(5 answers)
Closed 7 years ago.
I have an large textfile that contains an unique string in the middle. What i want to do is to print everything AFTER the string by using grep.
cat textfile | grep "target_string"
This highlights target_string but prints the whole file
cat textfile | grep -o "target_string"
This prints only target_string
cat textfile | grep -o "target_string*"
This prints only target_string
How can i print everything after target_string and nothing before?
Strangely, the accepted answer printed out the whole line, where I just wanted all the info after the target string. This worked for me:
sed -n 's/target_string//p' filename
Adapted from this post
With GNU grep, try -B0 -A999999999 or similar. A better choice might be awk:
awk '/target_string/ {seen = 1}
seen {print}'
If (your problem specification is slightly unclear) you don't also need to print the matching line, sed is even shorter:
sed '1,/target_string/d'
You forgot the '.':
cat textfile | grep -o "target_string.*"
This will print everything after each match, on that same line only:
perl -lne 'print $1 if /target_string(.*)/' textfile
This will do the same, except it will also print all subsequent lines:
perl -lne 'if ($found){print} else{if (/target_string(.*)/){print $1; $found++}}' textfile