Tag Info

New answers tagged

1

Here are a few ways of doing what you want: sed find . -iname "*.erb" -type f -exec \ sed -i '' '1s/^/#encoding:utf-8\n/' {} \; I don't have access to a BSD sed to check, so I can't guarantee that the \n will be read correctly. Try the command on a single file and without the -i first to make sure. Perl find . -iname "*.erb" -type f -exec \ perl ...


4

To edit file in-place with OSX sed, you need to set empty extension: $ sed -i '' '1i\ #encoding:utf-8' filename And you need a literal newline after i\. This is specified by POSIX sed. Only GNU sed allows text to be inserted on the same line with command. sed can also works with multiple files at once, so you can use -exec command {} + form: $ find . ...


0

Using Gnu Sed. -z option (Null separated records), sed slurps all the input as a single record. Please try: sed -ze 's/([^()]*)//g'


2

There is no need to mix many instruments. Task can be done by sed only sed '/^INFO\|^DEBUG\|^TRACE\|^ERROR/{ /Logger2/{ :1 N /\nINFO\|\nDEBUG\|\nTRACE\|\nERROR/!s/\n// $!t1 D } }' log.entry


1

Based on one answer at http://stackoverflow.com/questions/9605232/merge-two-lines-into-one this seems to fit the bill #!/usr/local/bin/bash PATTERN1='TRACE *'; PATTERN2='DEBUG *'; PATTERN3='INFO *'; PATTERN4='ERROR *'; LINEOUT="" while read line; do case $line in $PATTERN1) echo $LINEOUT LINEOUT="$line" ...


1

perl filter for multiline log records (record begin mark) Use the following perl script as a working prototype. Usage script_path regular_expression log_files e.g. script_path "line \d" log_file_1 log_file_2 #!/usr/bin/perl $pattern = qr/(?^s)$ARGV[0]/; shift; # process filtering expression # (?^s) - treats matched string as single line my $line = ''; # ...


1

You need to quote the argument, and use the s command to perform string substitution. You can use the -e option to supply multiple command expressions. sed -i -e 's/<% /[@/' -e 's/ %>/]/' filename


1

I'm not sure what you are trying to replace (with a - or _ in the first case) but this works fine for me: $ echo "<%_" | sed 's/<%_/_/' _ $ echo "<%_" | sed 's/<%_/-/' - $ echo "_%>" | sed 's/_%>/\]/' ] It looks like you are escaping the regex identifiers (the /).


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 ...


2

Firstly, this is a UUOC (useless use of cat). There is no good eason to use cat here, sed is perfectly able to read files itself, and even if it wasn't, then redirecting standard input from the file would be equivalent to piping it. esc=$(echo -e '\e') sed "s,\(.*\) \(.*\) \(.*\) \(.*\),$esc[31m\1 $esc[34m\2 $esc[33m\3 $esc[32m\4$esc[m," file4 This ...


0

Works for me with GNU sed (version 4.2.1): $ echo -ne $'\u9991' | sed 's/\xe9\xa6\x91//g' | hexdump -C $ echo -ne $'\u9991' | hexdump -C 00000000 e9 a6 91 (As another replacement for sed you could also use GNU awk; but it don't seem necessary.)


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.


14

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


0

Using sed Here is a sed solution: $ sed '\|// copyright|,\|^package|{s/^package/Something\nElse/p;d}' file Something Else com.base import com.base import com.base ... Did you want to remove all of the original package line? If so, just a minor change is needed: $ sed '\|// copyright|,\|^package|{s/^package.*/Something\nElse/p;d}' file Something Else ...


1

If your shell supports it, you could use a here document to read the commands from standard input, with a quoted delimiter to prevent shell expansion of the awk variable $5 awk -f- somefile << "EOF" {sum+=$5} END { print "Average = ",sum/NR} EOF Or just put the commands in a file and run that with awk -f.


1

You don't need '' (strong quotes), you can use the weaker form "", except you then need to escape the "s. awk "{sum+=\$5} END { print \"Average = \",sum/NR}" But why?


0

This awk version will keep entries intact that already have a user ID as part of the homeDirectory: awk -F ": *" ' $1=="uid" { uid = $2 } $1=="homeDirectory" && $2=="/home/myworker/2013" { $0=$0"/"uid } { print } ' infile > outfile (Note: your data should not have trailing spaces. To clean up that as well you can add a command { ...


2

A generic awk alternative: awk ' /^uid:/ { uid=$2 } /^homeDirectory:/ { if ($NF !~ uid"$") { $NF = $NF"/"uid } } { print } ' text.file


0

awk file : $1=="uid" { uid=substr($2,2) ; } $1=="homeDirectory" { printf "%s: /home/myworker/2013/%s\n",$1,uid ; next ;} { print ;} which basically remember uid print a fixed home dir, base on fixed year and last uid. to be called with awk -F: -f tmp.awk < yourinputfile where -F: use : as separator -f tmp.awk use the awk file


0

Use the following simple perl script to remove every pair of parentheses and their content, even across multiple lines: #!/usr/bin/perl undef $/; $text = <>; #Flags: g=match repeatedly; s=dot matches newline $text =~ s/\(.*?\)//gs; print $text; If you want to fit it into the commandline, here's the one-liner version: perl -p0777e 's/\(.*?\)//gs' ...


1

You need to look at the character before the percent. sed 's/\([^\\]\)%.*/\1/' If the previous character is not a backslash, keep that char and remove the rest. This answer assumes that the % does not appear at the beginning of the line. If it does, then we need to check for it sed 's/\(^\|[^\\]\)%.*/\1/'


1

For multi-line matching with sed, it's often easiest to read the whole file, and do your search/replace on the entire contents: sed -n ' # disable auto-printing 1h # first line, move to hold space 1!H # not the first line, append to hold space ${ # at the end of file x # move ...


1

To remain \new line (if parenthesis in two consequent lines) sed -e 's/\(^[^)]*) *\)\|([^)]*\() *\|$\)//g' filename The script consists three patterns: ^[^)]*) * from line begining any symbol(s) exept ) till ) with space(s) after; ([^)]* from ( any symbol(s) exept ) till ) with space(s) after; or till $ (end of line) For multiline cases: sed ...


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 ...


1

No. You can't use sed's or perl's -i switch to edit files in place in a read-only directory. As you correctly assumed, you won't be allowed to create the necessary temporary files: $ ls -ld read_only/ dr-xr-xr-x 2 terdon terdon 4096 Apr 13 02:16 read_only/ $ ls -l read_only/file -rw-r--r-- 1 terdon terdon 3 Apr 13 02:16 read_only/file $ sed -i 's/a/A/' ...


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.


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

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 ...


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. ...


2

\U is a GNUism, actually inspired from a BSDism since that comes from the equivalent s command in the ex editor (command-line mode of vi). OS/X sed most likely descends from FreeBSDs itself descending from 4.4BSD's sed, which was rewritten from scratch following copyright issues with AT&T. In any case, neither BSD nor AT&T sed support \U in their s ...


3

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


0

The simple task do not require sed while IFS=',' read -r first mid last do echo "$first, $mid, ${last^^}" done < file_with_commas If bash version do not support variable expansion you can use tr last=$(echo $last | tr [[:lower:]] [[:upper:]])


0

Seems /U and /L just do not work with OSX/BSD sed along with sed /pattern/,+2d # like `sed '/pattern/{N;N;d;}'` sed -n 0~3p # like `awk NR%3==0` sed /pattern/Q # like `awk '/pattern/{exit}1'` or `sed -n '/pattern/,$!p'` sed 's/\b./\u&/g' # \u converts the next character to uppercase sed 's/^./\l&/' # \l converts the next character to lowercase sed ...


2

Assuming a ZIP archive, as commonly found on MS‐DOS systems, under a unix command line prompt one might do: % unzip -p -a files.zip | awk ... where the ellipsis are replaced by your arguments to awk. Data input is received from standard input via the pipe. Replace unzip with the appropriate command if you're using another compression method and update ...


2

Much better if you show example input and desired output but in first close: sed ':1;N;/\w\n/{s/\n\([^\n]*\.\)/ \1/;t1};P;D'


2

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy. sed '\|*/|!{ s|/\*|\n&| #if ! */ repl 1st /* w/ \n/* h; s|foo|bar|g;/\n/!b #hold; repl all foo/bar; if ! \n branch G; s|\n.*\n||;:n #Get; clear difference; :new label n; ...


1

Using good old ed: ed /etc/pam.d/sshd << EOT /account required pam_nologin.so/ a account required pam_access.so . w EOT The /account required pam_nologin.so/ addresses a line. The a command is for appending text to the editor buffer. The text account required pam_access.so is entered into the buffer. . returns to ...


-1

I tried that as well, the situation I am having is I have 7 applications running on this server and each app uses a different IP, so what I am doing is if the app is app4 then do /sbin/ip addr ls eth0 | awk '/inet / {print $2, $8}' 133.8.5.9/16 eth0 133.8.5.8/16 eth0:1 133.8.5.7/16 eth0:2 133.8.5.6/16 eth0:3 133.8.5.5/16 eth0:4 133.8.5.4/16 eth0:5 ...


3

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


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


1

This should work (reading from stdin, printing to stdout for testin purposes): sed -e '/^account required pam_nologin\.so$/a account required pam_access.so' the a command will append the text to the matched line, the matched line being one consisting of exactly "account required pam_nologin.so". If you want to use the -i switch (edit ...


3

I think awk could help: awk 'BEGIN { sd = "20150408T13:29:28"; ed = "20150408T17:55:02"; } $1 "T" $2 >= sd && $1 "T" $2 <= ed' log


1

Certanly task can be done by sed sed '/20150408 13:29:28/,/20150408 17:55:02/! d' log_files but if lines did not have exact 20150408 13:29:28 script will print nothing and if its did not have exact 20150408 17:55:02 file will print every lines till the end. So the better is use date compare by script: limit1=$(date -d "20150408 13:29:28" +"%s") ...


0

The following is doing a more complex job: marking the first un-matched parentheses: #!/usr/bin/perl use strict; undef $/; $_= <>; # slurp input my $P = qr{[^()]*}; # $P = not parentheses # repace matched parentheses by marks while(s! \( ($P) \) !\cA$1\cB!gx){} while(s!^ ($P) \( (.*) \) ...


0

I've used awk -v BINMODE=rw '!($0 in a){a[$0];print}' infile >> outfile BINMODE=rw : to keep end of line terminators happy. (I live in a mixed os environment) Logic is simple. If the current line isn't in the associative array then add it to the associative array and print to output. There may be memory limitations with this approach. For very ...


0

With Python... here in one-liners executed from the shell instead of script: $ python -c "print 'H08W2345678'[3:]" W2345678 $ python -c "print 'H08W2345678'[:-3]" H08W2345


1

If you have a file in which every line is an eleven-character (or whatever) string that you want to chop up, sed is the tool to use.  It’s fine for manipulating a single string, but it’s overkill.  For a single string, Jason’s answer is probably the best, if you have access to bash.  However, the ${parameter:offset} and ${parameter:offset:length} syntaxes ...


5

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


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. $ ...


2

var=$(awk '/eth0:4/ {print $2}' file)"



Top 50 recent answers are included