GUIDA A SED, 06 – miscellanea

MISCELLANEA

DELETE LINES:

Delete a range of lines, from 2nd line till 4th line:
$ sed ‘2,4d’ file

Delete lines other than the specified range, line other than 2nd till 4th here:
$ sed ‘2,4!d’ file

Delete the first line AND the last line of a file, i.e, the header and trailer line of a file
$ sed ‘1d;$d’ file

Delete all lines beginning with a particular character, ‘L’ in this case
$ sed ‘/^L/d’ file

Delete all lines ending with a particular character, ‘x’ in this case:
$ sed ‘/x$/d’ file

Delete all lines ending with either x or X, i.e case-insensitive delete:
$ sed ‘/[xX]$/d’ file

Delete all blank lines in the fileDelete all lines which are empty or which contains just some blank spaces:
$ sed ‘/^$/d’ file

NNBB * significa 0 o molte ricorrenze dell’ultimo carattere (uno spazio in questo caso)

Delete all lines which are entirely in capital letters:
$ sed ‘/^[A-Z]*$/d’ file

Delete the lines containing the pattern ‘Unix’.
$ sed ‘/Unix/d’ file

Delete the lines NOT containing the pattern ‘Unix':
$ sed ‘/Unix/!d’ file

Delete the lines containing the pattern ‘Unix’ OR ‘Linux':
$ sed ‘/Unix\|Linux/d’ file
NB: The OR condition is specified using the | operator. In order not to get the pipe(|) interpreted as a literal, it is escaped using a backslash.

Delete the lines starting from the 1st line till encountering the pattern ‘Linux':
$ sed ‘1,/Linux/d’ file

Delete the lines starting from the pattern ‘Linux’ till the last line:
$ sed ‘/Linux/,$d’ file

Delete the last line ONLY if it contains the pattern ‘AIX':
$ sed ‘${/AIX/d;}’ file
NB: “$” is for the last line. To delete a particular line only if it contains the pattern AIX, put the line number in place of the $. This is how we can implement the ‘if’ condition in sed.

Delete the last line ONLY if it contains either the pattern ‘AIX’ or ‘HPUX':
$ sed ‘${/AIX\|HPUX/d;}’ file

Delete the lines containing the pattern ‘Solaris’ only if it is present in the lines from 1 to 4.
$ sed ‘1,4{/Solaris/d;}’ file

Delete the line containing the pattern ‘Unix’ and also the next line:
$ sed ‘/Unix/{N;d;}’ file
NB: “N” command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line.

Delete only the next line containing the pattern ‘Unix’, not the very line:
$ sed ‘/Unix/{N;s/\n.*//;}’ file
NB: Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.

Delete a range of lines from the pattern plus x lines:
$ sed ‘/Cygwin/,+2d’ file4

Ultimi 3 troppo complessi: non mi interessa:

http://www.theunixschool.com/2012/06/sed-25-examples-to-delete-line-or.html

PRINT LINES:

Print every alternate line:
$ sed ‘n;d’ file
“n” command prints the current line, and immediately reads the next line into pattern space. d command deletes the line present in pattern space. In this way, alternate lines get printed.

Print every 2 lines:
$ sed ‘n;n;N;d’ file
n;n; => This command prints 2 lines and the 3rd line is present in the pattern space. N command reads the next line and joins with the current line, and d deltes the entire stuff present in the pattern space. With this, the 3rd and 4th lines present in the pattern space got deleted. Since this repeats till the end of the file, it ends up in printing every 2 lines.

Print lines ending with ‘X’ within a range of lines:
$ sed -n ‘/Unix/,${/X$/p;}’ file
The range of lines being chosen are starting from the line containing the pattern ‘Unix’ till the end of the file($). The commands present within the braces are applied only for this range of lines. Within this group, only the lines ending with ‘x’ are printed.

Print range of lines excluding the starting and ending line of the range:
$ sed -n ‘/Solaris/,/HPUX/{//!p;}’ file
The range of lines chosen is from ‘Solaris’ to ‘HPUX’. The action within the braces is applied only for this range of lines. If no pattern is provided in pattern matching (//), the last matched pattern is considered. For eg, when the line containing the pattern ‘Solaris’ matches the range of lines and gets inside the curly braches, since no pattern is present, the last pattern (solaris) is matched. Since this matching is true, it is not printed(!p), and the same becomes true for the last line in the group as well.

To remove the 1st field or column :
$ sed ‘s/[^,]*,//’ file
This regular expression searches for a sequence of non-comma([^,]*) characters and deletes them which results in the 1st field getting removed.

To print only the last field, OR remove all fields except the last field:
$ sed ‘s/.*,//’ file
This regex removes everything till the last comma(.*,) which results in deleting all the fields except the last field.

To print only the 1st field:
$ sed ‘s/,.*//’ file
This regex(,.*) removes the characters starting from the 1st comma till the end resulting in deleting all the fields except the last field.

To delete the 2nd field:
$ sed ‘s/,[^,]*,/,/’ file
The regex (,[^,]*,) searches for a comma and sequence of characters followed by a comma which results in matching the 2nd column, and replaces this pattern matched with just a comma, ultimately ending in deleting the 2nd column.

NB: To delete the fields in the middle gets more tougher in sed since every field has to be matched literally.

To print only the 2nd field:
$ sed ‘s/[^,]*,\([^,]*\).*/\1/’ file

To print only the 2nd field:
$ sed ‘s/[^,]*,\([^,]*\).*/\1/’ file
The regex matches the first field, second field and the rest, however groups the 2nd field alone. The whole line is now replaced with the 2nd field(\1), hence only the 2nd field gets displayed.

Print only lines in which the last column is a single digit number:
$ sed -n ‘/.*,[0-9]$/p’ file
The regex (,[0-9]$) checks for a single digit in the last field and the p command prints the line which matches this condition.

To number all lines in the file:
$ sed = file | sed ‘N;s/\n/ /’
This is simulation of cat -n command. awk does it easily using the special variable NR. The ‘=’ command of sed gives the line number of every line followed by the line itself. The sed output is piped to another sed command to join every 2 lines.

Replace the last field by 99 if the 1st field is ‘Ubuntu':
$ sed ‘s/\(Ubuntu\)\(,.*,\).*/\1\299/’ file
This regex matches ‘Ubuntu’ and till the end except the last column and groups each of them as well. In the replacement part, the 1st and 2nd group along with the new number 99 is substituted.

Delete the 2nd field if the 1st field is ‘RedHat':
$ sed ‘s/\(RedHat,\)[^,]*\(.*\)/\1\2/’ file
The 1st field ‘RedHat’, the 2nd field and the remaining fields are grouped, and the replacement is done with only 1st and the last group , resuting in getting the 2nd field deleted.

To insert a new column in the beginning(1st column):
$ sed ‘s/.*/A,&/’ file
Same as last example, just the line matched is followed by the new column.

Note: sed is generally not preferred on files which has fields separated by a delimiter because it is very difficult to access fields in sed unlike awk or Perl where splitting fields is a breeze.

REMOVING CHARACTERS FROM A FILE

To remove 1st character in every line:
$ sed ‘s/^.//’ file
.(dot) tries to match a single character. The ^ tries to match a pattern(any character) in the beginning of the line. Another way to write the same:
$ sed ‘s/.//’ file
This tells to replace a character with nothing. Since by default, sed starts from beginning, it replaces only the 1st character since ‘g’ is not passed.

To remove last character of every line :
$ sed ‘s/.$//’ file
The $ tries to match a pattern in the end of the line.

To remove the 1st and last character of every line in the same command: $ sed ‘s/.//;s/.$//’ file

To remove first character only if it is a specific character:
$ sed ‘s/^F//’ file

To remove last character only if it is a specific character:
$ sed ‘s/x$//’ file

To remove 1st 3 characters of every line:
$ sed ‘s/…//’ file
A single dot(.) removes 1st character, 3 dots remove 1st three characters.

To remove 1st n characters of every line:
$ sed -r ‘s/.{4}//’ file
NB: .{n} -> matches any character n times, and hence the above expression matches 4 characters and deletes it.

To remove last n characters of every line:
$ sed -r ‘s/.{3}$//’ file

To remove everything except the 1st n characters in every line:
$ sed -r ‘s/(.{3}).*/\1/’ file
.* -> matches any number of characters, and the first 3 characters matched are grouped using parantheses. In the replacement, by having \1 only the group is retained, leaving out the remaining part.
——————–
——————–
NB: il raggruppamento può essere multiplo:
$ sed -r ‘s/(.{3})(.*)/\2/’ file4
O ancora:
$ sed -r ‘s/(.{3})(.*)/\2\1/’ file4
Darà come output per ogni riga prima la restante parte delle righe e poi i primi tre caratteri.
———————-
———————-
To remove everything except the last n characters in a file:
$ sed -r ‘s/.*(.{3})/\1/’ file

To remove multiple characters present in a file:
$ sed ‘s/[aoe]//g’ file
To delete multiple characters, [] is used by specifying the characters to be removed. This will remove all occurences of the characters a, o and e.

To remove a pattern:
$ sed ‘s/lari//g’ file

To delete only nth occurrence of a character in every line:
$ sed ‘s/u//2′ file
NB: By default, sed performs an activity only on the 1st occurence. If n is specifed, sed performs only on the nth occurence of the pattern.

To delete everything in a line followed by a character:
$ sed ‘s/a.*//’ file

To remove all digits present in every line of a file:
$ sed ‘s/[0-9]//g’ file
[0-9] stands for all characters between 0 to 9 meaning all digits, and hence all digits get removed

To remove all lower case alphabets present in every line:
$ sed ‘s/[a-z]//g’ file
[a-z] represents lower case alphabets range and hence all lower-case characters get removed

To remove everything other than the lower case alphabets:
$ sed ‘s/[^a-z]//g’ file
NB: ^ inside square brackets negates the condition. Here, all characters except lower case alphabets get removed.

To remove all alpha-numeric characters present in every line:
$ sed ‘s/[a-zA-Z0-9]//g’ file

To remove a character irrespective of the case:
$ sed ‘s/[uU]//g’ file
By specifying both the lower and upper case character in brackets is equivalent to removing a character irrespective of the case.

—————————————————-

INDICE

 

Lascia una risposta

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *

È possibile utilizzare questi tag ed attributi XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>