Output from grep commands won't write to a file - grep

My script is set up as follows
#!/bin/bash
grep 'zz' <$1> >>output.txt
grep '^.....'<$1> >>output.txt
grep '^...'|'^....' <$1> >>output.txt
grep 'ing$' <$1> >>output.txt
grep '^A' <$1> >>output.txt
grep '[oO]...t' <$1> >>output.txt
The goal of the shell script is to read from a given text file ($1) and write it to output.txt, however using the code as it is now throws an error that reads
line 2:syntax error near unexpected token '>>'
What is incorrect about my script? Thanks in advance everyone!

Related

Search through *.gz files with keeping the file name

Say I have multiple .gz files that I want to search a keyword in them. I can do this by piping zcat result to a grep like this:
zcat some.file.* | grep "keyword_1" | ... | grep "keyword_n"
The output of this command though will be just the matching line and won't have the file name in it. Is there any way I can attach the file name to the zcat output?
Try zgrep instead of zcat:
zgrep -H keyword some.file.*
And if you want to use egrep to get pattern matching:
export GREP=egrep
zgrep -H -e "(keyword1|keyword2)" some.file.*

How do I grep the list of results and print them with serial number?

I used a command in Sqoop to list the tables in my SQL Server that start with 'lkp' in table names
$sqoop list-tables | grep -i 'lkp'
What I need is I want to list the 'lkp' tables with serial numbers so I tried with the command
$sqoop list-tables | grep -in 'lkp'
but it resulted with numbers of entire list of tables irrespective of name 'lkp' as
7:LKP_AttributeType
11:LKP_CalendarName
22.LKP_CategoryError
27:LKP_ColumnDataType
38:LKP_ColumnName
and so on....
what I need is
1:LKP_AttributeType
2:LKP_CalendarName
3.LKP_CategoryError
4:LKP_ColumnDataType
5:LKP_ColumnName
Can any one explain me how to achieve that ?
Not familiar with sqoop, but can you use the "nl" command line tool to add line numbers to the grep output?
$sqoop list-tables | grep -i 'lkp' | nl
try this command line
sqoop list-tables | grep -i 'lkp'| awk '{print NR,$0}'

How to grep in one line starting from particular string to end with particular string

I want to grep "[calleruid]=aab01b055-89e3-49f3-839e-507bb128d07e&smscresponse"
in Below file
2014-10-15 18:38:32,831 plivo-rest[2781]: INFO: Fetching GET http://*******/outbound_callback.aspx with smscresponse[to]=8912722fsf9&smscresponse[ALegUUID]=5bb516fsd64-546c-11e4-879f-551816a551303677&smscresponse[calluid]=aab01b055-89e3-49f3-839e-507bb128d07e&smscresponse[direction]=outbosund&smscresfdsponse[endreason]=UNALLOCATED_NUMBER&smscresponse[from]=83339995896999&smscresponse[starttime]=0&smscresponse[ALegRequestUUID]=5bb4bafc-546c-11e4-891d-000c29ec6e41&smscresponse[RequestUUID]=5bb4bafc-546c-11e4-891d-000c29ec6e41&smscresponse[callstatus]=completed&smscresponse[endtime]=1413378509&smscresponse[ScheduledHangupId]=5bb4c15a-546c-11e4-891d-000c29ec6e41&smscresponse[event]=missed_call_hangup
I used this command
$ grep -oP '(calluid).*$'
this greps upto end of file
I used this command
$ grep -oP '(calluid).{40}'
it fetches 40 characters but i have 1000's of calleruid's so each have different no.s of characters
So please guide me to grep exact callerid data
Use a lookahead to force the regex engine to do the match upto a specific character or a boundary.
$ grep -oP '\[calluid\][^\]\[]*(?=\[|$)' file
[calluid]=aab01b055-89e3-49f3-839e-507bb128d07e&smscresponse
Here is an gnu awk (due to multiple characters in RS) version:
awk -v RS="[[]calluid[]]=" -F[ 'NR==2 {print $1}' file
aab01b055-89e3-49f3-839e-507bb128d07e&smscresponse
You can also set RS like this: RS="\\\[calluid]="

using grep to find the value of a string

I have file(file.txt) contains the following
aa=testing
bb=hello
cc=hi
Expected result
the value of aa is testing
How to use grep to find the value of aa?
Use a positive lookbehind in grep:
grep -Po "(?<=aa=).*" file.txt
Output
testing
grep -oP 'aa=\K.*' file.txt
Output:
testing
See: http://www.charlestonsw.com/perl-regular-expression-k-trick/
awk -F= '/^aa=/ { print $2 }' file
sed -n '/^aa=/s|^.*=||p' file
sed -n 's|^aa=||p' file
Output:
testing

Use grep to report back only line numbers

I have a file that possibly contains bad formatting (in this case, the occurrence of the pattern \\backslash). I would like to use grep to return only the line numbers where this occurs (as in, the match was here, go to line # x and fix it).
However, there doesn't seem to be a way to print the line number (grep -n) and not the match or line itself.
I can use another regex to extract the line numbers, but I want to make sure grep cannot do it by itself. grep -no comes closest, I think, but still displays the match.
try:
grep -n "text to find" file.ext | cut -f1 -d:
If you're open to using AWK:
awk '/textstring/ {print FNR}' textfile
In this case, FNR is the line number. AWK is a great tool when you're looking at grep|cut, or any time you're looking to take grep output and manipulate it.
All of these answers require grep to generate the entire matching lines, then pipe it to another program. If your lines are very long, it might be more efficient to use just sed to output the line numbers:
sed -n '/pattern/=' filename
Bash version
lineno=$(grep -n "pattern" filename)
lineno=${lineno%%:*}
I recommend the answers with sed and awk for just getting the line number, rather than using grep to get the entire matching line and then removing that from the output with cut or another tool. For completeness, you can also use Perl:
perl -nE 'say $. if /pattern/' filename
or Ruby:
ruby -ne 'puts $. if /pattern/' filename
using only grep:
grep -n "text to find" file.ext | grep -Po '^[^:]+'
You're going to want the second field after the colon, not the first.
grep -n "text to find" file.txt | cut -f2 -d:
To count the number of lines matched the pattern:
grep -n "Pattern" in_file.ext | wc -l
To extract matched pattern
sed -n '/pattern/p' file.est
To display line numbers on which pattern was matched
grep -n "pattern" file.ext | cut -f1 -d:

Resources