Shell learning: Exercises for grep, sed, awk commands

Source: Internet
Author: User
Tags uppercase letter

Files: datafile
Steve blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon deloach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Karen evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Fred fardbarkle:674-843-1385:20 Parak Lane, Deluth, MN 23850:4/12/23:780900
Fred fardbarkle:674-843-1385:20 Parak Lane, Deluth, MN 23850:4/12/23:780900
Lori gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram hardy:293-259-5395:235 Carltonlane, Joliet, IL 73858:8/12/20:56700
James ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara kertz:385-573-8326:832 Ponce Drive, Gary, in 83756:12/1/46:268500
Lesley kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose santiago:385-898-8357:38 Fife, Abilene, TX 39673:1/5/58:95600
Tommy savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

One, grep command practice
1. Show all rows that contain sans
grep San datafile
2. Show all the lines where the name of the person who started with J
grep "^j" datafile
3. Show all lines ending with 700
grep "700$" datafile
4. Show all lines that do not include 834
Grep-v "834" datafile
5. Show all birthdays on December lines
grep [:::]12[:/:] datafile or grep ' \:12\/' datafile
6. Display the line with area code 498 for all phone numbers
grep ' \:486\ ' datafile
7. Show all such lines: it contains an uppercase letter followed by four lowercase letters, a colon, a space, and an uppercase letter
grep [a-z][a-z][a-z][a-z][a-z][:::][[:space:]][a-z] datafile
8. Show rows whose last name begins with K or K
grep-i [a-z][[:blank:]]k datafile
9. Display a line with a salary of six digits, and add the line number in front
grep-n ' ([1-9]\{6,\}$ '
10. Displays rows that include Lincoln or Lincoln, and grep is not case sensitive.
Grep-i ' Lincoln ' datafile
Two SED command Big exercise
1. Change Jon's name to Jonathan.
sed-e ' S/jon/jonathan/datafile
2. Delete the first three rows
sed-e ' 1,3d ' datafile
3. Display 5-10 rows
sed-n ' 5,10p ' datafile
4. Delete the row that contains lane.
sed-e '/lane/d ' datafile
5. Show all birthdays between November-december lines
sed-n '/[:::]1[1-2][:/:]/p ' datafile
6. Add three asterisks (* * *) to the line starting with Fred
sed-e ' s/^fred/***fred/' datafile
7. Replace the row containing Jose with Jose has retired
sed-e ' s/^jose.*/jose has retire/g ' datafile
8. Change Popeye's Birthday to 11/14/46
Cat datafile | grep Popeye | sed-e ' s/3\/19\/35/11\/14\/46/'
9. Delete all blank lines
sed-e '/^$/d ' datafile
10. Write a script that will:
. Insert the title personnel FILE before the first line.
. Delete Wages ending in 500
. display file contents, reverse the surname and name
. Add the end at the end of the file

To create an SED script step:
1, create a script
Such as:

Code:
Avalee//home/javalee/x>cat Sedfile#this is Sedfile1ithis is example$ aend


2. Execute Sedfile script with Sed-f

Code:
Javalee//home/javalee/x>sed-f sedfile datafilethis is Examplesteve blenheim:238-923-7366:95 Latham Lane, Easton, PA 8 3755:11/12/56:20300....vinh tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900end


Three. awk Command Big Exercise
Files: datafile
Mike harrington:[510] 548-1278:250:100:175
Christian dobbins:[408] 538-2358:155:90:201
Susan dalsass:[206] 654-6279:250:60:50
Archie mcnichol:[206] 548-1348:250:100:175
Jody savage:[206] 548-1278:15:188:150
Guy quigley:[916] 343-6410:250:100:175
Dan savage:[406] 298-7744:450:300:275
Nancy mcneil:[206] 548-1278:250:80:75
John goldenrod:[916] 348-4278:250:100:175
Chet main:[510] 548-5258:50:95:135
Tom savage:[408] 926-3456:250:168:200
Elizabeth stachelin:[916] 440-1763:175:75:300

The above database contains names, phone numbers and donations in the last three months
1. Show All phone numbers
Awk-f ":" ' {print $} ' datafile
2. Show Dan's phone number
Awk-f ":" '/^dan/{print $ ' datafile
3. Show Susan's name and phone number
Awk-f ":" '/^susan/{print $1,$2} ' datafile
4. Show all surnames starting with D
Awk-f ":" ' {print $} ' datafile | awk ' {print $} ' |awk '/^d/'
5. Show all names beginning with a C or E
Awk-f ":" ' {print $} ' datafile | awk ' {print '} ' |awk '/^[ce]/'
6. Show all names with only four characters
Awk-f ":" ' {print $} ' datafile | awk ' {if (length ($) ==4) print $} '
7. Show all names with area code 916
Awk-f ":" '/\[916\]/{print $ ' datafile
8. Show Mike's donation. Each value is displayed with a $ start. such as $250$100$175
Awk-f ":" '/^mike/{print "$" $ "$" $ "$" $4 "$" $ $ "datafile
9. Display last name followed by a comma and name, such as Jody,savage
Awk-f ":" ' {print $} ' datafile | awk ' {print $, ', ', ' $ '
10. Write an awk script that functions as:
Displays the full name and phone number of the savage.
. Show donations for Chet
The names of all the first one-month donations are displayed.
Note: The area code is originally indicated by parentheses.
The script is as follows:
#!/bing/awk-f
Begin{fs= ":"}
{if ($ ~/savage/) print $ ":" $ $}
{if ($1}~/^chet/) print "$" $ "$": $ "$4": $ "$"
{if ($3==250) print $}
11. Write an awk script that functions
. Total contributions to the output of the owner
. Average number of contributions to output 12 persons
. Output the maximum number of individual quarterly donations
. Output the minimum value of a single quarterly donation
The script is as follows:

begin{
Fs= ":";
Total = 0;
Average = 0;
}

{
if (nr==1)
{
Max = $ $;
Min = $ $;
}

Total = $ $ + $4 + $;

for (i=3; i<=nf; ++i)
{
if ($i > Max)
{
max = $i;
}

if ($i < min)
{
min = $i;
}
}
}

end{
Average = TOTAL/12;
printf ("total =%d\n", total);
printf ("average =%d\n", average);
printf ("max =%d\n", max);
printf ("min =%d\n", min);
}

Shell learning: Exercises for grep, sed, awk commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.