0% found this document useful (0 votes)
21 views5 pages

Sed Grep Cmds 2

Uploaded by

subha mounika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Sed Grep Cmds 2

Uploaded by

subha mounika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Checking note

23 February 2023 11:36

1.To check two diffrent patterns at time ---sed '/patern1/,/patern2/' file_name


----sed -n -e '/pattern1/p' -e 'pattern2/p'
2. To replace the pattern in file globally ----sed 's/patern/replace_pattern/g'
3. To print entire data in file ---sed '1p' file_name
4. To print specific line of data in a file ----sed -n 'Np' ----here N is line number
5. To print the last line in a file ----sed -n '$p' file_name
6. To print the range of lines in a file ---sed -n '3,5p' file_name
7. To finde specific pattern ----sed -n '/is/p' file_name
8. To run the multiple expression ----sed -n -e '2p' -e '5p' file_name
----sed -n -e 'pattern1/p' -e 'pattern2/p' file_name
----sed -n '1p;6p' file_name
9. to see next N lines after the string found line ---sed -n '/pattern/,+3p' file_name
10. to see the N lines after the required line number ---sed -n '2,+4p' file_name
11. To print the even number lines or odd number lines ----sed -n '1~2p' file_name
12. To print the odd number lines o ------------------------------sed -n '2~2p' file_name
13. To print the equal distace lines -----------------------------sed -n '1~Np' file_name
14. To use the external file and that lines also ----------sed -n -f external_file_name file_name
15. To replace the patter in specific line --------sed '2 s/pattern1/replacing_pattern/g' file_name
16. to replace the pattern expcept that line ----sed '2! s/pattern1/replaceing_pattern/g' file_name
17. To modify the file for any operation use ------------------ -i
18. To search for pattern and modify that line only ---sed ' /search_string/ s/pattern1/replacing_pattern/g'
file_name
19. To delate a line ---------------sed '1d' file_name
20. To delete set of lines -----------sed '2,4d' file_name
21. To delete the last line ---------sed -n '$d' file_name
22. To delate the pattern from file ------sed '/pattern/d' file_name
23. To deleate empty line form -------sed '/^$/d' file_name
24. To replace space with tab --------sed 's/ /\t/g' file_name
25. To replace the tab with space ----------sed 's/\t/ /g' file_name
26. To copy the specific pattern line into another file ------sed '/pattern/ w new_file_name' file_name
27. To add the new line date after the line specified ------sed '3 a write_date_here ' file_name
28. To add the new line date after the pattern found ----sed '/pattern/ a wirte_date_here' file_name
29. To change the specified line --------sed '3 c wirte_line_content' file_name
30. To change the line if specified pattern is matched ------sed '/pattern/ c write_the_line_content' file_name
31. To insert the data in certain position --------sed ' 3 i write_content_of_line' file_name
32. To insert the data in multiple positions at a time ---sed -e '3 i write_data' -e '1 i write_data' file_name
33. To see the hidden charecters in the file like \t $....---sed -n 'l' file_name
34. To trim the line upto the length --------sed -n 'l 10' file_name
35. To read the data from external file add the date in present file ---sed '3 r external_file ' file_name
36. To read the data from external file and and the data if pattern is find ---sed '/pattern/ r external_file'
file_name
37. To add the date in your file in specific line-----sed '2 e date' file_name
38. To set the line numbers --------sed '='file_name
39. To get the start string matched with specific pattern in line -------sed '/^pattern/p' file_name
40. To get the line end with particular pattern --------------------------sed '/patterm$/p' file_name
41. To print the line spicfie charcter matches -------------------sed '/[AC]/p' file_name
42. To print the line specified character starts with -----------sed '/^[AC]/p' file_name
43. To print the line specified chacters starts with range ------sed '/^[A-C]/p' file_name
44. Filter like find alphanumaric lower case ,uppercase ,digits ,punctionmarcked lines -----sed '/[[:punct:]]/p'
file_name
^ start of line
$ end of line
. single character
[] match character set
[^] exclusive Set
* zero or more occurance

[:alnum:]
[:alpha:]
[:digit:]
[:blank:]
[:lower:]
[:upper:]
[:punct:]
[:space:]

45.find and replace the string case senstive -----------------sed 's/senstive/SS/i' file_name
46.find and replace the string in file by taking backup of original file ---------sed -i.back 's/sensitive/sjkj/g'

Grep ccommand:

Certainly! The grep command in Linux is used to search for specific patterns or strings within files or directories.
Here's an overview of how to use grep:

Basic Syntax:

css
Copy code
grep [options] pattern [file...]
Commonly Used Options:

-i: Perform a case-insensitive search.


-v: Invert the match, i.e., display lines that do not match the pattern.
-r or -R: Recursively search directories and their contents.
-l: Only display the names of files that contain the pattern.
-n: Display line numbers along with matching lines.
-w: Match whole words only.
-c: Display a count of matching lines instead of the lines themselves.
Examples:

Search for a Pattern in a File:

grep pattern file.txt


This will search for the specified pattern in the file file.txt and display all matching lines.

Case-Insensitive Search:

grep -i pattern file.txt


This will perform a case-insensitive search for the pattern in the file.
Search in Multiple Files:

grep pattern file1.txt file2.txt


This will search for the pattern in both file1.txt and file2.txt.

Recursive Search in Directories:

grep -r pattern directory


This will search for the pattern in all files within the specified directory and its subdirectories.

Invert the Match:

grep -v pattern file.txt


This will display all lines in file.txt that do not contain the specified pattern.

Display File Names Only:

grep -l pattern file1.txt file2.txt


This will only display the names of files that contain the pattern.

Search for a pattern recursively in specific file types:

grep -r --include="*.txt" pattern directory


This will perform a recursive search for the pattern within all files with the ".txt" extension in the specified directory
and its subdirectories.

Search for lines that start with a specific word:

grep '^word' file.txt


This will display all lines in file.txt that start with the specified word.

Search for lines that end with a specific word:

grep 'word$' file.txt


This will display all lines in file.txt that end with the specified word.

Search for lines that contain a word boundary:

grep '\<word\>' file.txt


This will display all lines in file.txt that contain the exact word "word" with word boundaries.

Search for lines that match multiple patterns using OR logic:

grep -E 'pattern1|pattern2' file.txt


This will display all lines in file.txt that match either "pattern1" or "pattern2".

Search for lines that match multiple patterns using AND logic:

grep 'pattern1' file.txt | grep 'pattern2'


This will display all lines in file.txt that match both "pattern1" and "pattern2".
Count the number of occurrences of a pattern in multiple files:

grep -c pattern file1.txt file2.txt


This will display the count of how many times the pattern occurs in each file separately.

Search for lines that do not contain a specific pattern:

grep -v 'pattern' file.txt


This will display all lines in file.txt that do not contain the specified pattern.

Search for lines that match a pattern and display the surrounding context:

grep -C 2 'pattern' file.txt


This will display the lines containing the pattern in file.txt, as well as 2 lines of context before and after each match.

Search for lines that match a pattern and display line numbers:

grep -n 'pattern' file.txt


This will display all lines in file.txt that contain the pattern, along with their line numbers.

Use regular expressions to perform advanced pattern matching:

grep -E '[0-9]{3}-[0-9]{3}-[0-9]{4}' file.txt


This will search for phone numbers in the format xxx-xxx-xxxx in file.txt.

Search for lines that start with a specific pattern and redirect the output to a file:

grep '^pattern' file.txt > output.txt


This will search for lines in file.txt that start with the specified pattern and save the results in output.txt.

Search for a pattern in compressed files:

zgrep 'pattern' file.gz


This will search for the pattern in a compressed file (file.gz) using zgrep, which is specifically used for compressed
files.

These examples demonstrate some additional use cases for the grep command. You can explore the grep manual
page (man grep) for more information on its various options and advanced features.

How to see column 2 or 3?


awk '{print $2}' file_name

How to see multiple coulumns?


awk '{print $2,$3}' file_name

How to see last column?


awk '{print $NF}' file_name

How to see line no.?


awk '{print NR}' file_name

How to see line no. with - ?


awk '{print NR "- " $2}' file_name
How to get a column from CSV?
awk -F, '{print $7}' country.txt

How to change the salary of Pol?


awk '{if($2=="Pol"){$3="90000"} print $0}' file_name

How to see data of users whose salary is more than 40000?


awk '{if($3>40000) print $0}' file_name

How to see a line whose length of character is more than 15?


awk 'length($0)>15' file_name

How to see data of Indian users?


awk '/India/ {print}' file_name

How to see a specific line example 3rd line?


awk 'NR==3 {print}' file_name

How to see range of lines, 3 to 5th line?


awk 'NR==3,NR==5 {print}'

How to see which lines are empty?


awk 'NF==0 {print NR}' file_name

How to check no. of lines in the file?


awk 'END {print NR}' file_name

How to use for loop in AWK command?


awk 'BEGIN {for(i=0;i<=10;i++) print i;}'

How to use while loop in AWK command?


awk 'BEGIN {while(i<10){ i++; print "Num is " i;}}'

How to see only 2nd char?


cut -c2 file_name

How to see 1 to 4 char?


cut -c1-4 file_name

How to see only 2 and 4th char?


cut -c2,3 file_name

How to see data from CSV?


cut -d: -f 3 file_name

How to change the delimeter of output?


cut -d, -f 1- country.txt --output-delimiter="|"

How to use cut command with other commands like cat?


Using pipe |

You might also like