text2 is same as above
terminal:
1) grep using the regular expression "Hello"
2) With awk command, for each record, as long as it match regular expression "Hello", we execute the default action, print out.
3) For each matched record, we output with a special format including filename, record number(line number) and record itself.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ grep Hello text2
1 Hello
3 Hello
5 Hello
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '/Hello/' text2
1 Hello
3 Hello
5 Hello
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '/Hello/ { print FILENAME,":", FNR,":", $0 }' text2
text2 : 1 : 1 Hello
text2 : 3 : 3 Hello
text2 : 5 : 5 Hello
2. Line restriction search
text2 is same as above
terminal:
1) -e means command option. For each record, sed will firstly try to print it out. -n disable the option. Next step, if the record satisfy the condition(line 1 to line 4), p option means printing it out. So the command means print out the first 4 lines of records.
2) For each record, if it satisfies the condition: line number is 1 to 4, and match the regular expression "Hello", then awk execute the default action: print it out.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ sed -n -e 1,4p text2
1 Hello
2 World
3 Hello
4 Chicago!
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '(FNR>=1) && (FNR<=4) && /Hello/' text2
1 Hello
3 Hello
3. Swap columns
text2 is same as above
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '{ print $2,$1; }' text2
Hello 1
World 2
Hello 3
Chicago! 4
Hello 5
4. Convert the column separators
text2 is same as above
terminal:
At the "BEGIN" section, we set up the "OFS" variable to tab, for each input record, as long as we changed the value of one field, the $0 will be assembled with each field and new OFS variable, in this case, tab. After assignment of $1, first field, $0 get changed too.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk 'BEGIN { OFS="\t"; } { $1=$1; print; }' text2
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
5. Convert carriage-return/newline line terminator to newline terminator:
text2 is same as above
terminal:
We use carriage-return/newline as the record separator to retrieve records. And then use print command to print out record, and "print" command will add the newline operator in the end.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk 'BEGIN { RS="\r\n"; } { print; }' text2
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
6 Convert single-space line to double-space lines
text2 is same as above
terminal:
1) First Command is changing output record separator to double newline operator, then for each record, awk execute the action to print out the record plus the double newline operator.
2)Second Command also changed the output record separator to double new line operator. Then for each record, pattern "1" means always true, also means for each record, awk execute default action, print out the record + output record separator.
3) Third command doesn't change the output record separator, it is still single new line operator. But the action use two print command to add two output record separators.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk 'BEGIN { ORS="\n\n"; } { print; } ' text2
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk 'BEGIN { ORS="\n\n"; } 1' text2
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '{ print;print "";}' text2
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
No comments:
Post a Comment