Tag Info

Hot answers tagged

14

Just use that syntax: sed 's/馑//g' file1 Or in the escaped form: sed "s/$(echo -ne '\u9991')//g" file1


12

Perl can do that: echo 汉典“馑”字的基本解释 | perl -CS -pe 's/\N{U+9991}/Jin/g' -CS turns on UTF-8 for standard input, output and error.


11

You are forgetting that the default action of sed is to print each pattern space (line) - so suppress the default behaviour you need to add the -n switch sed -n '1,4p' list


10

Use this: sed 's/|END|/\n/g' test.txt What you attempted doesn't work because sed uses basic regular expressions, and your sed implementation has a \| operator meaning “or” (a common extension to BRE), so what you wrote replaces (empty string or END or empty string) by a newline.


8

You need the write permission on a directory to create or remove files in it, but not to write to a file in it. Most shell commands, when given an output file, simply open that file for writing, and replace the data that was previously in the file. The > redirection operator truncates the existing file (i.e. deletes the existing file content, resulting in ...


7

You can use Bash's parameter expansion: string="foo-bar-123" && printf "%s\n" "${string##*-}" 123 If you want to use another process, with Awk: echo "foo-bar-123" | awk -F- '{print $NF}' Or, if you prefer Sed: echo "foo-bar-123" | sed 's/.*-//' A lighter external process, as Glenn Jackman suggests is cut: cut -d- -f3 <<< "$string" ...


7

The following worked fine for me: $ sed 's/|END|/\ /g' foobar T|somthing|something T|something2|something2 Notice that I just put a backslash followed by the enter key.


5

You can use awk: $ awk -F'\\|END\\|' '{$1=$1}1' OFS='\n' file T|somthing|something T|something2|something2 -F'\\|END\\|' set field separator to |END| OFS='\n' set ouput field separator to newline $1=$1 cause awk reconstruct $0 with OFS as field separator 1 is a true value, causeawk print the whole input line


5

Another perl: print a line if there are 3 commas. perl -i.bak -ne 'print if tr/,/,/==3' file The tr operator returns the number of characters transliterated.


5

I doubt it will make a difference but, just in case, here's how to do the same thing in Perl: perl -ne 'print if ++$k{$_}==1' out.txt If the problem is keeping the unique lines in memory, that will have the same issue as the awk you tried. So, another approach could be: cat -n out.txt | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- How it works: ...


5

Just using bash: string="H08W2345678" echo "${string:3}" W2345678 echo "${string:0:-4}" H08W234 See the Wooledge wiki for more on string manipulation.


4

Starting off with a basic file here... $ cat file location 100 Number location 101 Number location 102 Number We can match on all lines: $ awk '{print $0 NR}' file location 100 Number1 location 101 Number2 location 102 Number3 it gets more complicated if there is filler somewhere in the middle. You have to have a separate counter tallying up the ...


4

The -i/--in-place flag edits a file in place. By default, sed does the (arguably) simplest possible thing and doesn't check if the file is a symlink, before reading from it, transforming, then truncating and writing. GNU sed has a --follow-symlinks flag, which makes it behave as you want with symlinks: $ echo "cat" > pet $ ln --symbolic pet pet_link $ ...


4

In-place sed requires making a backup file during the process. The -i option on Apple's sed requires an extension argument (for the backup file it creates) and consumes the next argument. That means you're telling it you want it to make a backup file with the extension "#s</head>#...". The error means it thinks you're referring to the append command. ...


4

A number of versions of sed support Unicode: Heirloom sed, which is based on "original Unix material". GNU sed, which is its own codebase. Plan 9 sed, which has been ported to Unix-like operating systems. I couldn't find information on BSD sed, which I thought was strange, but I think the odds are good that it supports Unicode too. Unfortunately, there ...


3

Having GNU sed you can use the following command: sed -nr 's/([^ ]+) (.*)/echo "\1" \2\n/ep' input Which outputs: keyword value value keyword value value keyword Linux with your input data. Explanation: The sed command suppresses regular output using the -n option. -r is passed to use extended regular expressions which saves us some escaping of ...


3

2 thoughts: with sed, for any line that ends with a carriage return, join the next line sed '/\r$/ {N; s/\r\n//} ' file with awk, define the record separator for input and output: awk -v RS='\r\n' -v ORS='' 1 file


3

You're looking for tail : ls -ltr | tail -n 1 This will display only the last line of ls -ltr's output. You can control the number of lines by changing the value after -n; if you omit -n 1 entirely you'll get ten lines.


3

Another possibly awk command and using its RS option would be: awk '$1=$1' RS="\|END\|" file Will print those records (based on awk's Record Separator) which are not empty( has at least one field) to prevent printing empty lines. Tested on this input: T|somthing|something|END|T|something2|something2|END| Test|END| |END| Gives this output: ...


3

Because the \a character is Ctrl+G -- you don't need to escape the character after \n For maintainability, I'd recommend slightly reducing the one-liner-ness of it, and use actual newlines to continue the a command. This also enables the blank line you want. sed -i -e '$a\ \ # Provide apache user permissions to run the ban_ip.sh script as part of ...


3

You can use awk: awk -F',' 'NF==4' file If you can use gawk version >= 4.1.0 you can use inplace, more info. So it could be: gawk -i inplace -v INPLACE_SUFFIX=.bak -F',' 'NF==4' file


3

With perl: $ perl -F, -i.bak -ane 'print if @F > 3' file With perl > 5.20, you can use -F without -a and -n (-F implies -a and -a implies -n). Or you can use sed: $ sed -i.bak -e '/\([^,]*,\)\{3,\}/!d' file


3

sed ' s/.*/YES(&)/;:t s/([^()]*)//g;tt s/.....*/NO/'


3

#!/usr/bin/perl use DB_File; tie %h, 'DB_File'; while(<>){ not $h{$_} and print and $h{$_}=1 } EDIT 1: Does it really work? (comparing) Sol1 : Terdon et all Schwartzian-transform-like one-liner cat -n _1 | sort -uk2 | sort -nk1 | cut -f2- Sol2 : perl + DB_File (this answer) perl dbfile-uniq _1 Sol3 : PO (John W. Gill solution has a ...


3

$ echo "H08W2345678" | sed 's/^.\{3\}//' W2345678 sed 's/^.\{3\}//' will find the first three characters by ^.\{3\} and replace with blank. Here ^. will match any character at the start of the string (^ indicates the start of the string) and \{3\} will match the the previous pattern exactly 3 times. So, ^.\{3\} will match the first three characters. $ ...


3

Use this: sed -n 1,4p list or this: sed '1,4!d' list More examples


3

sed 'h;s/[^,]*,[^,]*,// y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ H;x;s/[^,]*\n// ' foo.file


3

My apologies, but reading your statement The exact text I want to insert after end of the HEAD section is... led me initially to believe we were talking about appending text to file section rather than inserting before - so I wrote that answer first. After rereading the question, and more closely studying your example sed command - I think I understand this ...


3

If the directory is read-only but the files within that directory are read/write, then there is nothing stopping you from overwriting those files. From a script you can write to the files using usual redirection > and >> as well as overwriting them using cp. What you cannot do is to create a new file in the directory and rename it on top of an ...


3

Perl's Tie::File module offers true in-place edit functionality: perl -MTie::File -e ' tie @a,"Tie::File","your_file_here"; # Do something... ' This makes the elements of @a into the lines of your file and any changes done to @a are reflected in the file even if the file is in a read-only directory.



Only top voted, non community-wiki answers of a minimum length are eligible