Linux Tips and Tricks

Linux Tips and Tricks

Dot or Source Operator – Unix/Linux

May3

Dot or Source Operator

Read and execute commands from the filename argument in the current shell context.

A period `.’ is a synonym for `source’

Create a file called test.cfg and enter the below contents

1
2
3
4
5
$ cat test.cfg
hostname=mymachine.com
password=password
ftpname=abc
ftppassword=xyz

 

1
2
3
4
5
$ . test.cfg
 
or
 
$ source test.cfg

Accessing the Values

1
2
3
4
5
6
7
8
9
10
11
$ echo $hostname
mymachine.com
 
$ echo $password
password
 
$ echo $ftpname
abc
 
$ echo $ftppassword
xyz

Accessing the Function from the file

1
2
3
4
5
$ cat test.func
function abc
(
    echo "I am inside the abc function"
)

 

1
2
3
4
5
$ . test.func
 
or
 
$ source test.func

 

1
2
3
#Calling the function abc
$ abc
I am inside the abc function
posted under Uncategorized | No Comments »

Google Code Jam 2012 – Problem A. Speaking in Tongues

April15

Problem

We have come up with the best possible language here at Google, called Googlerese. To translate text into Googlerese, we take any message and replace each English letter with another English letter. This mapping is one-to-one and onto, which means that the same input letter always gets replaced with the same output letter, and different input letters always get replaced with different output letters. A letter may be replaced by itself. Spaces are left as-is.

For example (and here is a hint!), our awesome translation algorithm includes the following three mappings: ‘a’ -> ‘y’, ‘o’ -> ‘e’, and ‘z’ -> ‘q’. This means that “a zoo” will become “y qee”.

Googlerese is based on the best possible replacement mapping, and we will never change it. It will always be the same. In every test case. We will not tell you the rest of our mapping because that would make the problem too easy, but there are a few examples below that may help.

Given some text in Googlerese, can you translate it to back to normal text?

Solving this problem

Usually, Google Code Jam problems have 1 Small input and 1 Large input. This problem has only 1 Small input. Once you have solved the Small input, you have finished solving this problem.

Input

The first line of the input gives the number of test cases, T. T test cases follow, one per line.

Each line consists of a string G in Googlerese, made up of one or more words containing the letters ‘a’ – ‘z’. There will be exactly one space (‘ ‘) character between consecutive words and no spaces at the beginning or at the end of any line.

Output

For each test case, output one line containing “Case #X: S” where X is the case number and S is the string that becomes G in Googlerese.

Limits

1 ≤ T ≤ 30.
G contains at most 100 characters.
None of the text is guaranteed to be valid English.

Sample

1
2
3
4
5
Input
3
ejp mysljylc kd kxveddknmc re jsicpdrysi
rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd
de kr kd eoya kw aej tysr re ujdr lkgc jv

Output
1
2
3
 Case #1: our language is impossible to understand
Case #2: there are twenty six factorial possibilities
Case #3: so it is okay if you want to just give up

Solution :

save the below contents into one file ( mappings.txt )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
a=y
b=h
c=e
d=s
e=o
f=c
g=v
h=x
i=d
j=u
k=i
l=g
m=l
n=b
o=k
p=r
r=t
s=n
t=w
u=j
v=p
w=f
x=m
y=a
q=z
z=q

save the below file as script.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cat script.sh
#!/bin/bash
 
#source the mappings
. mappings.txt
 
inputfile=$1
while read line
do
echo "$line" | while read -n1 char
        do eval echo -n \$char
        done
echo -e "\n"
done < $inputfile | sed 's,\$, ,g' | nawk 'BEGIN{i=0}NR>1 && /./ {i++;printf("Case #%d: %s\n",i,$0)}'

 

Pass the input file as parameter and execute the script.

1
2
3
4
$ ./script.sh input.txt
Case #1: our language is impossible to understand
Case #2: there are twenty six factorial possibilities
Case #3: so it is okay if you want to just give up

 

 

posted under Uncategorized | No Comments »

Basic operations using Awk and Sed

March15

awk and sed

Swap the columns using the sed and awk

1
2
3
4
5
$ echo "A B" | sed 's/\(.\) \(.\)/\2 \1/'
B A
 
$ echo "A B" | awk '{print $2,$1}'
B A

Replace the first occurance of “AA” with “ZZ”

1
2
3
4
5
$ echo "ABCDAB" | awk '{sub("AB","ZZ",$0)}1'
ZZCDAB
 
$ echo "ABCDAB" | sed 's/AB/ZZ/'
ZZCDAB

Replace all the occruances of “AA” to “ZZ”

1
2
3
4
5
$ echo "ABCDAB" | awk '{gsub("AB","ZZ",$0)}1'
ZZCDZZ
 
$ echo "ABCDAB" | sed 's/AB/ZZ/g'
ZZCDZZ

Print all the charcters with space delimited

1
2
3
4
5
$ echo "ABCDAB" | awk -v FS= '{for(i=1;i<=NF;i++){printf("%s ",$i)}}'
A B C D A B 
 
$ echo "ABCDAB" | sed 's/./& /g'                                                
A B C D A B

Print the 2nd line of the test.txt file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat test.txt
one
two
three
four
five
 
$ sed -n '2p' test.txt
two
 
$ awk 'NR==2' test.txt
two
 
$ sed '2!d' test.txt
two

Print all the lines except the 2nd line

1
2
3
4
5
6
7
8
9
10
11
$ sed '2d' test.txt
one
three
four
five
 
$ awk 'NR!=2' test.txt
one
three
four
five

Print the 2nd,3rd and 4th line in the test.txt file

1
2
3
4
5
6
7
8
$ sed -n '2,4p' test.txt
two
three
four
 
$ awk 'NR>1&&NR
 
<strong>Replace the wore "one" to "two" in the 2nd line of the file.</strong>
1
2
3
4
5
6
7
8
9
10
11
12
13
$ sed -e '2 s/one/two/' test.txt
one
two
three
four
five
 
$ awk 'NR==2{sub("one","two",$0)}1' test.txt
one
two
three
four
five
posted under Uncategorized | No Comments »

Exploring echo and Colorful echo

March13

echo command

echo  command is used to places the text in the terminal or console

1) Create a file using the echo command.

1
$ echo "The Linux Tips" > myfile.txt

2) Append the text in the existing file

1
2
3
4
5
6
7
8
$ cat myfile.txt
The Linux Tips
 
$ echo "The Linux Tips - Second Line" >> myfile.txt
 
$ cat myfile.txt
The Linux Tips
The Linux Tips - Second Line

3) echo and its arguments

-n -> It will not output the trailing newline

1
2
$ echo -n "The Linux Tips"
The Linux Tips$

Normal one ( trailing newline )

1
2
3
$ echo "The Linux Tips"
The Linux Tips
$

-e -> It will enable the interpretation of backslash escapes

1
2
3
4
#In the below example, i used \n (new line), but without using -e it prints the \n
 
$ echo "The Linux \n Tips "
The Linux \n Tips
1
2
3
$ echo -e "The Linux \n Tips"
The Linux
 Tips
1
2
3
4
5
6
#In the below example, i used \t (tab), but without using -e it prints the \t
$ echo "The Linux\tTips"
The Linux\tTips
 
$ echo -e "The Linux\tTips"
The Linux    Tips

With effective to -e option, we can use the below options

1
2
3
#print the \ (back slash)
 
$ echo -e "\\"

\

1
2
3
4
# \b is for backspace. In the below example, you can see the d character is removed
 
$ echo -e "abcd\bef"
abcef

 

1
2
3
4
# \c is used produce no further output. In the below example, ef is not printed
 
$ echo -e "abcd\cef"
abcd$

 

1
2
3
4
# \e is used for escape
 
$ echo -e "abcd\eef"
abcdef

 

1
2
3
4
5
# \f is for form feed.
 
$ echo -e "abcd\fef"
abcd
    ef

 

1
2
3
4
# \r is for carriage return
 
$ echo -e "abcd\ref"
efcd

 

1
2
3
4
# \t is for horizontal tab
 
$ echo -e "abcd\tef"
abcd    ef

 

1
2
3
4
5
# \v is for vertical tab
 
$ echo -e "abcd\vef"
abcd
    ef

-E -> disable interpretation of backslash escapes (default)

1
2
3
4
5
6
$ echo -E "The Linux \n Tips"
The Linux \n Tips
 
$ echo -e "The Linux \n Tips"
The Linux
 Tips

echo and its colors

The below program is using ANSI escape code SGR sequences. ( For more about the SGR sequence, search “Ansi escape code + wiki” in google )

Type the below program and save it as colors.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cat colors.sh
#!/bin/sh
FGRED=`echo "\033[31m"`
FGCYAN=`echo "\033[36m"`
BGRED=`echo "\033[41m"`
FGBLUE=`echo "\033[35m"`
BGGREEN=`echo "\033[42m"`
 
NORMAL=`echo "\033[m"`
 
echo "${FGBLUE} Text in blue ${NORMAL}"
echo "Text normal"
echo "${BGRED} Background in red"
echo "${BGGREEN} Background in Green and back to Normal ${NORMAL}"

Assign the execute permission and execute the script.  code tag didn’t show the colors 🙁 in this page

1
2
3
4
5
$ ./colors.sh  
 Text in blue
Text normal
 Background in red
 Background in Green and back to Normal

After executing the script, you can see some colorful text and background colors

Change the numbers after the “[” ( opening square bracket ) and see the difference

For more information about colors, see here

Try with other numeric combinations and see different colors 🙂

 

bye
kamaraj

posted under Uncategorized | No Comments »

Awk Basics & Tutorial – 2

March3

Awk Basics & Tutorial – 2

Today, we will see some awk pattern matching.

create a input.txt file with the below contents. we are going to use the input.txt for all our awk commands.

1
2
3
4
5
6
7
$ cat input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
scripting language consisting of a set of actions to be taken against
textual data (either in files or data streams) for the purpose of
producing formatted reports. The language used by awk extensively uses the
string datatype, associative arrays (that is, arrays indexed by key strings),
and regular expressions.

In the input.txt, i want to print only the lines which have the word “awk”, then we can use the below commands.

# In the below command /pattern/ is any normal word or regular expression.
awk ‘/pattern/’ input.txt

1
2
$ awk '/awk/' input.txt
producing formatted reports. The language used by awk extensively uses the
1
2
$ awk '/awk/{print}' input.txt
producing formatted reports. The language used by awk extensively uses the
1
2
$ awk '/awk/{print $0}' input.txt
producing formatted reports. The language used by awk extensively uses the

How to ignore the case and match the pattern ?

we can use the tolower or toupper function in awk to ignore the case and print the lines.

1
2
3
$ awk 'tolower($0)~/awk/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
producing formatted reports. The language used by awk extensively uses the
1
2
3
$ awk 'toupper($0)~/AWK/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
producing formatted reports. The language used by awk extensively uses the

# GNU awk has the special varibale called IGNORECASE. In default it set to 0 (means not case-sensitive)
# In the below command, i used -v ( used to define the variable name and initialize the variable value )

1
2
3
$ awk -v IGNORECASE=1 '/awk/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
producing formatted reports. The language used by awk extensively uses the
1
2
3
$ awk 'BEGIN{IGNORECASE=1}/awk/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
producing formatted reports. The language used by awk extensively uses the

we can also inclue the upper case and lower case in the square brackets and find the pattern.

1
2
3
$ awk '/[Aa][Ww][Kk]/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
producing formatted reports. The language used by awk extensively uses the

Now, we can see how to match two or more patterns in a file.

we can use && and || in the condition.

How to search a word “the” and “AWK” in the same line of a file ?

1
2
$ awk '/the/ && /awk/' input.txt
producing formatted reports. The language used by awk extensively uses the
1
2
3
4
# Ignore the case (GNU AWK)
$ awk '{IGNORECASE=1} /the/ && /AWK/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
producing formatted reports. The language used by awk extensively uses the

To search a any one of the pattern, then use ||

1
2
3
4
5
# Ignore the case (GNU AWK)
$ awk '{IGNORECASE=1} /the/ || /AWK/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
textual data (either in files or data streams) for the purpose of
producing formatted reports. The language used by awk extensively uses the
1
2
3
$ awk '/the/ ||  /awk/' input.txt
textual data (either in files or data streams) for the purpose of
producing formatted reports. The language used by awk extensively uses the

How to find a line which starts with “t” or “T” ?

1
2
3
4
# Here ^ is used to mention the start of the line
$ awk '/^[tT]/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
textual data (either in files or data streams) for the purpose of
1
2
3
4
#GNU AWK
$ awk '{IGNORECASE=1}/^t/' input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
textual data (either in files or data streams) for the purpose of

How to find a line which ends with specific word ?

You can use a special character “$” to match the pattern which is there in the end of the line.

1
2
3
4
5
$ cat one.txt
one two three
two three one
three two one
one three two
1
2
3
4
# Find the line which ends with the word "one"
$ awk '/one$/' one.txt
two three one
three two one
1
2
3
# Find the line which ends with the word "two"
$ awk '/two$/' one.txt
one three two

Hope you guys enjoyed and learned something about the pattern matching ( basics ) in this blog post.

will see some more basics in the next blog.

bye
Kamaraj

posted under Uncategorized | No Comments »

Awk Basics & Tutorial – 1

March1

Awk Basics & Tutorial – 1

What is AWK ?

The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data (either in files or data streams) for the purpose of producing formatted reports. The language used by awk extensively uses the string datatype, associative arrays (that is, arrays indexed by key strings), and regular expressions.

For more theory about AWK, just google it.

Create a text file (input.txt) with the below contents

1
2
3
4
5
6
7
$ cat input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
scripting language consisting of a set of actions to be taken against
textual data (either in files or data streams) for the purpose of
producing formatted reports. The language used by awk extensively uses the
string datatype, associative arrays (that is, arrays indexed by key strings),
and regular expressions.

AWK Structure

pattern {action}

BEGIN and END patterns in the AWK

BEGIN { print “BEGIN” }
{ print }
END   { print “END”  }

Exmaple :

1
2
3
4
5
6
7
8
9
$ awk 'BEGIN{print "BEGIN"}{print}END{print "END"}' input.txt
BEGIN
The AWK utility is a data extraction and reporting tool that uses a data-driven
scripting language consisting of a set of actions to be taken against
textual data (either in files or data streams) for the purpose of
producing formatted reports. The language used by awk extensively uses the
string datatype, associative arrays (that is, arrays indexed by key strings),
and regular expressions.
END

In the above example you can see that word “BEGIN” came in the First line and the word “END” came at the last line.

BEGIN and END is the special pattern, which is not used to match the records.

BEGIN block always execute before reading the file. In the below example, i am giving the file which is not exists in my current path. Eventhough the BEGIN block executes properly.

1
2
3
$ awk 'BEGIN{print "BEGIN"}{print "TEST"}' aaaaaaaaaa.txt
BEGIN
awk: fatal: cannot open file `aaaaaaaaaa.txt' for reading (No such file or directory)

END block always executes, once the file read is completed (file is processed fully)

Now, we see how to print particular columns in the input file.

The default delimiter for the awk is space.

$N – here i represent the N as column position.

1
2
3
4
5
6
# Contents of the in.txt
 
$ cat in.txt    
AAA 123
BBB 234
CCC 456
1
2
3
4
5
6
# Print the First Column in the in.txt
 
$ awk '{print $1}' in.txt
AAA
BBB
CCC
1
2
3
4
5
6
# Print the Second Column in the in.txt
 
$ awk '{print $2}' in.txt
123
234
456

 

1
2
3
4
5
6
# Swap the columns and print the in.txt
 
$ awk '{print $2,$1}' in.txt
123 AAA
234 BBB
456 CCC

If the file is seperated with some delimeter, then how to print the columns ?

we have a special option called -F for awk. we can used this option to specify the delimiter.

In the below example, the input file is using the pipe ( | ) as delimiter

1
2
3
4
$ cat in.txt
AAA|123
BBB|234
CCC|456

Why we are using -F\| ( back slash + | ) ?
All the special characters needs to be escaped.

1
2
3
4
$ awk -F\| '{print $1}' in.txt
AAA
BBB
CCC

 

1
2
3
4
$ awk -F\| '{print $2}' in.txt
123
234
456

 

1
2
3
4
$ awk -F\| '{print $2,$1}' in.txt
123 AAA
234 BBB
456 CCC

In some cases, we dont know how many fields (columns) are there in the input file. In that case, how to print the last column or last before column ?

we have a special variable called NF (number of fileds)

so, we can print the last filed using $NF and last before column as $(NF-1)

1
2
3
4
5
6
7
$ cat input.txt
The AWK utility is a data extraction and reporting tool that uses a data-driven
scripting language consisting of a set of actions to be taken against
textual data (either in files or data streams) for the purpose of
producing formatted reports. The language used by awk extensively uses the
string datatype, associative arrays (that is, arrays indexed by key strings),
and regular expressions.

 

1
2
3
4
5
6
7
8
9
#Prints the number of fields in each line
 
$ awk '{print NF}' input.txt
14
12
12
11
11
3

 

1
2
3
4
5
6
7
8
9
#Prints the last field in the line
 
$ awk '{print $NF}' input.txt
data-driven
against
of
the
strings),
expressions.

 

1
2
3
4
5
6
7
8
9
#Prints the last before field in the line.
 
$ awk '{print $(NF-1)}' input.txt
a
taken
purpose
uses
key
regular

How to print the line number in the awk ?

we have special variable called NR. This holds the line number which gets processed.

1
2
3
4
5
6
7
$ awk '{print NR}' input.txt
1
2
3
4
5
6

 

1
2
3
4
5
6
7
$ awk '{print NR,$0}' input.txt
1 The AWK utility is a data extraction and reporting tool that uses a data-driven
2 scripting language consisting of a set of actions to be taken against
3 textual data (either in files or data streams) for the purpose of
4 producing formatted reports. The language used by awk extensively uses the
5 string datatype, associative arrays (that is, arrays indexed by key strings),
6 and regular expressions.

you can notice $0 in the above command. what is that ?

$0 is used to print the whole line.

If we are using print alone in the block, then it will print the whole line

1
2
3
4
$ awk '{print}' in.txt
AAA|123
BBB|234
CCC|456

So, today you learned about the below things about awk.

1) AWK pattern
2) BEGIN block
3) END block
4) Print the particular colmns
5) -F argument
6) NF variable
7) NR variable

I will write some other basic things in the next blog.

– Kamaraj

posted under Uncategorized | No Comments »

GNU Grep command examples

February29

Today we will see some GNU grep options.

you can find detailed documentation for GNU grep here http://www.gnu.org/software/grep/

Type the below command

1
$ grep --help

It will display the grep options with details. we will discuss some of the options from the list

1
2
3
4
5
6
7
8
9
10
11
12
13
-s, --no-messages suppress error messages
-n, --line-number print line number with output lines
-v, --invert-match select non-matching lines
-H, --with-filename print the filename for each match
-h, --no-filename suppress the prefixing filename on output
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
 
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM

-s, –no-messages suppress error messages

1) we are going to use the -s option now in grep.
2) try to grep some word from the non-existence file.

1
2
3
4
5
6
7
8
$ ls one.txt
ls: cannot access one.txt: No such file or directory
 
$ grep "GNU" one.txt
grep: one.txt: No such file or directory
 
$ echo $?
2

By using the -s we can suppress the “No such file or directory” error message. But still you can see the command failed (by checking the $?)

1
2
3
$ grep -s "GNU" one.txt
$ echo $?
2

Create a test.txt with the below contents. we are going to use the test.txt for testing the grep command with the above options.

1
2
3
4
5
6
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.

-n, –line-number print line number with output lines

It will print the line number of the matched line.

#In the below command, it will match any character in the line

1
2
3
4
5
6
7
8
9
10
11
12
$ grep -n . test.txt
1:This is the test file for grep command
2:GNU grep is having more options and it is easy to use
3:The grep command searches one or more input files for lines
4:containing a match to a specified pattern.
5:By default, grep prints the matching lines.
 
#grep the word "command" in the test.txt
 
$ grep -n "command" test.txt
1:This is the test file for grep command
3:The grep command searches one or more input files for lines

-v, –invert-match select non-matching lines

As the description says, it will print the non-matching lines from the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -n "GNU" test.txt
2:GNU grep is having more options and it is easy to use
 
$ grep -vn "GNU" test.txt
1:This is the test file for grep command
3:The grep command searches one or more input files for lines
4:containing a match to a specified pattern.
5:By default, grep prints the matching lines.

-H, –with-filename print the filename for each match

-H option will print the filename for each match of the grep command output.

see the differene between the -H and the normal grep command.

1
2
3
4
5
6
7
$ grep "command" test.txt
This is the test file for grep command
The grep command searches one or more input files for lines
 
$ grep -H "command" test.txt
test.txt:This is the test file for grep command
test.txt:The grep command searches one or more input files for lines

-h, –no-filename suppress the prefixing filename on output

-h option is used to suppress the filename. ( when you are trying to grep with more than one file )
If you are using -h with one file name, then it will behave like the normal grep command.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cp test.txt test1.txt
 
$ grep "command" test*
test.txt:This is the test file for grep command
test.txt:The grep command searches one or more input files for lines
test1.txt:This is the test file for grep command
test1.txt:The grep command searches one or more input files for lines
 
$ grep -h "command" test*
This is the test file for grep command
The grep command searches one or more input files for lines
This is the test file for grep command
The grep command searches one or more input files for lines

-o, –only-matching show only the part of a line matching PATTERN

-o option is used to print only the matched pattern from the line.

see the difference between the below two commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ grep "in[a-z]" test.txt
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -o "in[a-z]" test.txt
ing
inp
ine
ini
int
ing
ine

-q, –quiet, –silent suppress all normal output

-q option is used to suppress the output of grep command.

It is more equal to redirect the output of the grep command to /dev/null

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ grep -q "abcd" test.txt
$ echo $?
1
 
$ grep -q "command" test.txt
$ echo $?
0
 
$ grep "command" test.txt >/dev/null 2>&1
$ echo $?
0
 
$ grep "abcd" test.txt >/dev/null 2$ echo $?
1

-B, –before-context=NUM print NUM lines of leading context

-B is used to print the matched line and the N number of lines before the match.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
#The word matching is present in the last line of the test.txt. so it also prints 2 lines above the matched line.
 
$ grep -B 2 "matching" test.txt
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.

-A, –after-context=NUM print NUM lines of trailing context

-A is same like -B, but it will print after the N number of lines, it matched.

1
2
3
4
5
6
7
8
9
10
11
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -A 2 "GNU" test.txt
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.

-C, –context=NUM print NUM lines of output context

-C is combination of -A and -B

1
2
3
4
5
6
7
8
9
10
11
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -C 1 "search" test.txt
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.

 

posted under Uncategorized | No Comments »

Facebook Hacker Cup 2012 – Alphabet Soup solution

January26

Alfredo Spaghetti really likes soup, especially when it contains alphabet pasta. Every day he constructs a sentence from letters, places the letters into a bowl of broth and enjoys delicious alphabet soup.

Today, after constructing the sentence, Alfredo remembered that the Facebook Hacker Cup starts today! Thus, he decided to construct the phrase “HACKERCUP”. As he already added the letters to the broth, he is stuck with the letters he originally selected. Help Alfredo determine how many times he can place the word “HACKERCUP” side-by-side using the letters in his soup.

Input

The first line of the input file contains a single integer T: the number of test cases. T lines follow, each representing a single test case with a sequence of upper-case letters and spaces: the original sentence Alfredo constructed.

Output

Output T lines, one for each test case. For each case, output “Case #t: n”, where t is the test case number (starting from 1) and n is the number of times the word “HACKERCUP” can be placed side-by-side using the letters from the sentence.

Constraints

  • 1 < T ≤ 20
  • Sentences contain only the upper-case letters A-Z and the space character
  • Each sentence contains at least one letter, and contains at most 1000 characters, including spaces

Example input

1
2
3
4
5
6
5
WELCOME TO FACEBOOK HACKERCUP
CUP WITH LABEL HACKERCUP BELONGS TO HACKER
QUICK CUTE BROWN FOX JUMPS OVER THE LAZY DOG
MOVE FAST BE BOLD
HACK THE HACKERCUP

Example Output

1
2
3
4
5
Case #1: 1
Case #2: 2
Case #3: 1
Case #4: 0
Case #5: 1

Original Input File:

Download the original input file from here

Output for Original File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Case #1: 82
Case #2: 1
Case #3: 15
Case #4: 15
Case #5: 0
Case #6: 2
Case #7: 58
Case #8: 3
Case #9: 13
Case #10: 11
Case #11: 17
Case #12: 10
Case #13: 24
Case #14: 76
Case #15: 48
Case #16: 2
Case #17: 60
Case #18: 1
Case #19: 49
Case #20: 3

Shell Script
If you are not getting the desired output then use nawk or gawk

1
2
3
4
5
6
#!/usr/bin/ksh
fileName="$1"
awk 'NR>1' $1 | while read line
do
	echo $line | nawk '/H/&&/A/&&/C/&&/K/&&/E/&&/R/&&/C/&&/U/&&/P/' | fold -w1 | sort | uniq -c | nawk '{if($0~/C$/){a=$1/2}else{a=$1}printf("%i %s\n",a,$2)}'| sort -n | egrep "H|A|C|K|E|R|C|U|P" > out.txt && nawk  '{if(NR==1){print $1}}' out.txt  || echo "0"
done | awk '{printf("Case #%s: %s\n",NR,$1)}'
posted under Uncategorized | No Comments »

Awk and Grep

December29

Awk and Grep

we are going to see how the grep and awk works for the same functionality

1
2
3
4
5
6
7
8
9
10
11
$ cat test.txt
one
two
three
four
five
six
seven
eight
nine
ten

1) Grep the pattern using grep and awk

1
2
3
4
5
6
7
8
9
$ grep "o" test.txt
one
two
four
 
$ awk '/o/' test.txt
one
two
four

2) Print the pattern line number using grep and awk

1
2
3
4
5
6
7
8
9
$ grep -n "o" test.txt
1:one
2:two
4:four
 
$ awk '/o/ {print NR":"$0}' test.txt
1:one
2:two
4:four

3) Print the number of occurences of in a file using grep and awk

1
2
3
4
5
$ grep -c "o" test.txt
3
 
$ awk '/o/ {i++}END{print i}' test.txt
3

4) Invert the sense of matching, to select non-matching lines using grep and awk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ grep -v "o" test.txt
three
five
six
seven
eight
nine
ten
 
$ awk '!/o/' test.txt
three
five
six
seven
eight
nine
ten

5) Print the filename if pattern found in the file.

1
2
3
4
5
6
7
8
9
10
11
$ grep -l "one" *
armstrong
armstrong1
test.pl
test.txt
 
$ awk '$0~/one/{a[FILENAME]=1}END{for(i in a)print i}' *
armstrong1
test.pl
test.txt
armstrong

6) grep multiple patterns using grep and awk

1
2
3
4
5
6
7
$ grep -E "one|two" test.txt
one
two
 
$ awk '/one/ || /two/' test.txt
one
two
posted under Uncategorized | No Comments »

Do’s and Dont’s in shell scripting

December22

Do’s and Dont’s in shell scripting

1) Dont use Grep and Cat command together

Wrong way :

1
$cat filename | grep "pattern"

Correct way :

1
$grep "pattern" filename

2) Dont use cat,grep and wc commands together

Wrong way :

To count the number of occurence

1
$cat filename | grep "pattern" | wc -l

Correct way :

1
$grep -c "pattern" filename

3) dont use sort and uniq together ( to eliminate the duplicate entires )

Wrong way :

1
$sort filename | uniq

Correct way :

1
$sort -u filename

4) Dont use grep and awk together

wrong way :

1
$ grep "pattern" filename | awk '{print $1}'

correct way :

1
$ awk '/pattern/ {print $1}' filename

5) Dont use ls in for loop ( to manipulate the files )

wrong way :

1
2
for i in `ls` ; do echo $i; done
for i in `ls *.txt`; do echo $i; done

Correct way :

1
2
for i in *; do echo $i; done
for i in *.txt; do echo $i; done
posted under Uncategorized | No Comments »
« Older EntriesNewer Entries »

Recent Comments

    Categories