sed

regular expression in sed for masking credit card

We need to mask credit card numbers.Masking all but last 4 digits. I am trying to use SED. As credit card number length varies from 12 digits to 19,I am trying to write regular expression.Following code will receive the String. If it contains String of the form "CARD_NUMBER=3737291039299199", it will mask first 12 digits. Problem is how...

How to remove the filename from the end of the du -h output

Hi, take the example: $ du -h file_size.txt 112K file_size.txt How would i remove the filename from the output of du -h I have tried to use sed to search for a string (the filename) and replace it with nothing, but it hasnt worked (command below) du -h file_size.txt | sed 's/ 'file_size.txt'//' Could someone please point out why...

Sed removing duplicate characters and certain characters in beginning/end of string

Hello, I am asking for your help with sed. I need to remove duplicate underscores and underscores from beginning and end of string. For example: echo '[Lorem] ~ ipsum *dolor* sit metus !!!' | sed 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._()-]/_/g' Produces: _Lorem____ipsum__dolor__sit_metus____ But I ne...

Using sed to replace a string with the contents of a variable, even if it's an escape character.

I'm using sed -e "s/\*DIVIDER\*/$DIVIDER/g" to replace *DIVIDER* with a user-specified string, which is stored in $DIVIDER. The problem is that I want them to be able to specify escape characters as their divider, like \n or \t. When I try this, I just end up with the letter n or t, or so on. Does anyone have any ideas on how to do this...

replace with sed on Mac OS X Leopard doesn't do what expected

I'm trying to replace \" (backslash doouble quote) by ' (quote) using sed. sed "s/\\\"/\'/g" file.txt The command doesn't work as expected. It replaces all the " in the text file, not only the \". It does the same thing as sed "s/\"/\'/g" file.txt I'm working on Mac OS X Leopard. Does any one have a clue? ...

Find lines that don't begin with a "<", perform action

Am using vim and have a large text file that contains some html thrown in throoghout. Am trying to prepare it for the web and need to add <p></p> tags to the lines that are not yet formatted. Here is an example of what I have: Paragraph text one one line [... more ... ] Other paragraph text on the next line [... more ... ] <h1>html ...

grep and sed command question

Hi there i have a truckload of files with sql commands in them, i have been asked to extract all database table names from the files How can I use grep and sed to parse the files and create a list of the unique table names in a text file ..one per line? the name names all seem to start with "db_" which is handy! what would be the best ...

sed error - unterminated substitute pattern

I am in directory with files consisting of many lines of lines like this: 98.684807 :(float) 52.244898 :(float) 46.439909 :(float) and then a line that terminates: [chuck]: cleaning up... I am trying to eliminate :(float) from every file (but leave the number) and also remove that cleaning up... line. I can get: sed -ie 's/ :(fl...

Awk, Sed : How to parse and sum values from a string

Hi all, I've a string like this Cpu(s): 1.9%us, 2.1%sy, 1.5%ni, 94.5%id, 0.8%wa, 0.0%hi, 0.1%si, 0.0%st it represents the Cpu usage of my unix box. Now I need to apply awk and sed (i think) to extract the current load of my CPUs. I'd like to extract the 'us', 'sy', 'ni' values from the string and then I want to sum them. The s...

Regular expression using sed

Hi experts, In the past I have asked this question.But somehow I did not give the complete input.Input is a log file. I am trying to use sed to replace all but last four digits of credi card number. sed -e :a -e "s/[0-9]\([0-9]\{4\}\)/\*\1/;ta" $today_temp_log This expression definitely works but it replaces not just Credit card numbe...

N-Insert In Sed

How would I replace the first 150 characters of every line with spaces using sed. I don't want to bring out the big guns (Python, Perl, etc) and it seems like sed is pretty powerful in itself (some people have written the dc calculator in it for example). A related problem is inserting 150 spaces in front of every line. s/^.\{50\}/?????...

Use sed to replace first 8 and last 4 pipes on every line in a file

Here's the situation, I have a text file that is pipe-delimited and one of fields contains pipe characters. I already have a sed script that will change it to be tab-delimited, but the problem is it's terribly slow. It will replace the first occurrence of a pipe 8 times, then replace the last occurrence of a pipe 4 times. I'm hoping ther...

how to remove leading whitespace from each line in a file?

I have a file that looks something like this: for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) I want it to look like this (remove indentations): for (i = 0; i < 100; i++) f...

Script for changing C++ class names

I have moved my classes from a global namespace into a specific namespace. I have also changed the class names. I want to convert all my source files that use these classes to the new format. I was thinking either a bash script using a sed file on Cygwin or executing a perl script. I'm having troubles with the bash script. Here's th...

Variations of sed between OSX and GNU/Linux

Hi all- I've got a makefile (developed for gmake on Linux) that I'm attempting to port to OSX, but it seems like sed doesn't want to cooperate. What I do is use GCC to autogenerate dependency files, and then tweak them a bit using sed. The relevant portion of the makefile: $(OBJ_DIR)/%.d: $(SRC_DIR)/%.cpp $(CPPC) -MM -MD $< -o $@ ...

Shell Script, Search File for String

I'm writing a shell script that opens a file and needs to find a tag like ##FIND_ME##. The string I'm searching for is a constant (and there is only ever one instance of it.) Once I locate that string, I need it to start a new search for a different string from that point forward. My *nix skills are a little rusty, should try to imple...

How do you escape a user-provided search term that you don't want evaluated for sed?

I'm trying to escape a user-provided search string that can contain any arbitrary character and give it to sed, but can't figure out how to make it safe for sed to use. In sed, we do s/search/replace/, and I want to search for exactly the characters in the search string without sed interpreting them (e.g., the '/' in 'my/path' would not ...

Extracting data from a file

I have a file results.txt which is like: a.txt {some data} success!! b.txt {some data} success!! c.txt {some data} error!! I want to extract data from it. I want an output like: a.txt: success b.txt: success c.txt: error The problem is that the {some data} part can be arbitrarily long. How can this be done? ...

sed calculations

Hi. I want to parse a css file and multiply each pixel value by (2/3). I was wondering if this was possible with sed? I know this is incorrect syntax but i think it'll bring home the explanation of what i want to achieve: sed -e "s|\([0-9]*\)px|int((\1 * 2)/3)|g" file.css So basically I want to take \1, multiply it by (2/3) and cas...

What is the most compact or efficient way of doing several subsitutions in a file in bash

Hi, I have a file data.base which looks like: 1234 XXXX 4321 XXXX 9884 ZZZZ 5454 YYYY 4311 YYYY 9882 ZZZZ 9976 ZZZZ ( ... random occurrences like this till 10000 lines) I would like to create a file called data.case which derives from data.base just with substitutions of XXXX, YYYY, ZZZZ for float numbers. I wonder what would be th...