sed

Simple way to insert line to particular position in file

I need to insert string to text file, for example, before first line from the end. STRINGS=`wc -l $OLDFILE \ | awk '{print $1-1}' \ | sed "s/$DOLLAR/+/g" \ | tr -d \\\n \ | sed "s/+$DOLLAR//" \ | bc` ADDFILE=$3 head -n $STRINGS $OLDFILE > $NEWFILE cat $ADDFILE >> $NEWFILE tail -n 1 $OLDFILE >> $NEWFILE Can you sugg...

sed in one folder

hello, i need 'sed' for some batch; now there are a lot of requirements; could anyone tell me how to put all the requirements and sed itself to run in one dir?? [so no need for install, that it works] 'Cause i need to get the batch file public, all that requirements seem to give a lot of work for the users... ...

What is logical AND in Sed/Vim?

I would like to get an AND -operator to the following Vim command :command! -nargs=1 GrepSource :! grep <args> `sed -n 's/^source \(.*\)/\1/p' %` such that I can use the command to find sources at # source <path> # . <path> source <path> . <path> I am not sure whether it is possible or not to get AND -operator to SED or Vim. How ...

Match any character (including newlines) in sed

I have a sed command that I want to run on a huge, terrible, ugly html file that was created from a microsoft word document. All it should do is remove any instance of the string style='text-align:center; color:blue; exampleStyle:exampleValue' The sed command that I am trying to modify is sed "s/ style='[^']*'//" fileA > fileB It w...

How to pass long strings for search and replace in a bash, sed, or rpl script?

I am working to create a script which will take a string as an argument and replace recursively in a directory. The simple case (a single word) is admirably handled by the following find and replace script: grep -rl $1 . | xargs sed -i .backup -e "s/$1/$2/g" But here things get a bit more tricky. The string I am trying to deal with...

using sed and grep to search and replace

I am using egrep -R followed by a regular expression containing about 10 unions, so like: .jpg | .png | .gif etc... This works well. I would like to then replace all strings found with .bmp I was thinking of something like egrep -lR "\.jpg|\.png|\.gif" . | sed "s/some_expression/.jpg/" file_it_came_form so the issue here is, how do I...

Manipulating huge CSV files with sed

I have a set of 4 massive CSV files that I need to modify. What I need to do is match this expression /^(.*),,/ copy the atom then prepend it to every subsequent line until the atom is matched again. Then I need to rinse and repeat until the end of the file (each file has approx 25k lines in it). Finally I need to go back through and r...

shell script to link specific files up one level

I've got a folder that's got a whole lot of folders inside, which each contain a movie file. Now I want to be able to see all these movies at one time (but I have to preserve the folder structure) so I wanted to symlink them all to the top level folder (flat view in Directory Opus would be the go here, but I'm on a mac). Here's my attemp...

How do you use shell script variables as arguments to sed?

What I would like to do is something like the following: #!/bin/sh EMAIL="-e 's/SOMETHING//g'" somecommand | sed "$EMAIL" But I get the following: sed: -e expression #1, char 2: unknown command: `'' I've tried many variations. I know it just a matter of getting the string quoting right. The reason I'd like to do this is to break...

Odd Sed Error Message

bash-3.2$ sed -i.bakkk -e "s#/sa/#/he/#g" .* sed: .: in-place editing only works for regular files I try to replace every /sa/ with /he/ in every dot-file in a folder. How can I get it working? ...

Using sed to grab filename from full path?

I'm new to sed, and need to grab just the filename from the output of find. I need to have find output the whole path for another part of my script, but I want to just print the filename without the path. I also need to match starting from the beginning of the line, not from the end. In english, I want to match, the first group of char...

How do I replace a token with the result of `pwd` in sed?

I'm trying to do something like this: sed 's/#REPLACE-WITH-PATH/'`pwd`'/' Unfortunately, I that errors out: sed: -e expression #1, char 23: unknown option to `s' Why does this happen? ...

How to insert a new line character after a fixed number of characters in a file

I am looking for a bash or sed script (preferably a one-liner) with which I can insert a new line character after a fixed number of characters in huge text file. ...

excluding first and last lines from sed /START/,/END/

Consider the input: =sec1= some-line some-other-line foo bar=baz =sec2= c=baz If I wish to process only =sec1= I can for example comment out the section by: sed -e '/=sec1=/,/=[a-z]*=/s:^:#:' < input ... well, almost. This will comment the lines including "=sec1=" and "=sec2=" lines, and the result will be something like: #=sec...

Inserting text to a file with Sed within Bash Script

I tried to insert a text to the first line of a file using sed. I do this inside a bash script. But why it hangs at the line of sed execution? #! /bin/bash # Command to execute # ./mybashcode.sh test.nbq nbqfile=$1 nbqbase=$(basename $nbqfile nbq) taglistfiletemp="${nbqbase}taglist_temp" taglistfile="${nbqbase}taglist" ./myccod...

Column to row translation using sed

I'm writing a bash script to get data from procmail and produce an output CSV file. At the end, I need to translate from this: ---label1: 123456 ---label2: 654321 ---label3: 246810 ---label4: 135791 ---label5: 101010 to this: label1,label2,label3,label4,label5 123456,654321,246810,135791,101010 I could do this easily with a ruby s...

removing extensions in subdirectories

I need to remove the extension ".tex": ./1-aoeeu/1.tex ./2-thst/2.tex ./3-oeu/3.tex ./4-uoueou/4.tex ./5-aaa/5.tex ./6-oeua/6.tex ./7-oue/7.tex Please, do it with some tools below: Sed and find Ruby Python My Poor Try: $find . -maxdepth 2 -name "*.tex" -ok mv `sed '[email protected]@@g' {}` {} + ...

Extract text from hostname

Using OS X, I need a one line bash script to look at a client mac hostname like: 12345-BA-PreSchool-LT.local Where the first 5 digits are an asset serial number, and the hyphens separate a business unit code from a department name followed by something like 'LT' to denote a laptop. I guess I need to echo the hostname and use a combinat...

sed multiline replace

this is my sample text file : asdas //<<<TAG this should be removed //TAG>>> this should be there //<<<TAG T > asd asd //TAG>>> for which i want o/p as : asdas this should be there Basically i m trying to find lines between "//<<>>" (including these lines too) and delete them. I tried using sed sed -n '1h;1!H;${;g;s/\/\/...

find and replace

Hello, Find: regexp1 **sometext** regexp2 Replace with: newregexp1 **sometext** newregexp2 Here, I do not want **sometext** to be modified. That is to say, if I have the following lines in a file: Hello somebody! Have a good day. Hello somebodyelse! Have a good day. I want the output to be: Bye somebody! Good night. Bye so...