String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.

learn more… | top users | synonyms

8
votes
3answers
9k views

Bash - test for newline?

In various places around the web I've found: \015 \012 \x0a - hex \n \r all as synonyms for various newlines / carriage returns... But in this small script I cannot recognise when I come across a ...
2
votes
1answer
462 views

Replace complex string in several files

A hacker got into a web server and added this string (removed some characters for security purposes and added line breaks for readability) in all index.php files: <?php ...
5
votes
3answers
4k views

Most common encoding for strings in C++ in Linux (and Unix?)

For creating a C++ program that is source code level portable between Windows and Linux and handles internationalization well, there are IMHO three main encodings to consider: The encoding of the ...
2
votes
4answers
300 views

Generate lists of page numbers for 2-up duplex printing: 2,3,… and 1,4,…

How can I generate the following increments? 2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47,...,135 and ...
7
votes
7answers
2k views

Bash - Continuous String Manipulation

#!/bin/bash FILE="$(basename "$1")" FILE="${FILE/%.jpeg/.jpg}" Is there anyway to glue these two lines together into a one-liner?
2
votes
3answers
3k views

Bash string replace multiple chars with one

I'm replacing, from a feed title, all chars except letters and digits with a dash to use the result as a safe filename for any filesystem: $ t="Episodie 06: No hope of riding home (NEW) - Advanced ...
1
vote
1answer
248 views

Check a bash variable against a file

I have a bash variable ($HTTP_COOKIE) which contains a string. I want to check if this string matches the content of a given file (token). I'm working on a busybox linux. How should I do?
3
votes
3answers
3k views

How to pass a string parameter on bash function?

I have this code that does work: get_parameter () { echo "$query" | sed -n 's/^.*name=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" } But I want to replace the "name" with the parameter that I pass ...
2
votes
3answers
139 views

expr help - managing strings

I need to take text like this: A234321=http://www.google..... a normal URL And pull out only the URL, getting rid of the first part. I think I can use expr to do it, but I can't figure out the ...
5
votes
2answers
3k views

grep surrounding characters of a match

I'm looking to do a find and replace within a giant database dump, and it's not doing what I think should happen. I'd like to grep for my target string in the file, and then see the surrounding 8 ...
2
votes
3answers
143 views

How to pass lines from a file to a bash script, so that each line remains undivided, even if there are spaces?

Given: $ cat lines.txt a/b 'c/d e/f' $ cat 1.sh #!/bin/sh ./2.sh `cat lines.txt` $ cat 2.sh #!/bin/sh echo p1=$1 echo p2=$2 echo p3=$3 $ ./1.sh p1=a/b p2='c/d p3=e/f' How do I change lines.txt ...
3
votes
2answers
185 views

filename with no spaces from two command lines

I am running two Linux commands and I want to put the resulting values together in a filename with no spaces and a dot separating the two values. So far, I've got this: pid=`sudo dmidecode -s ...
2
votes
2answers
9k views

Bash shell script to locate and remove substring within a filename

I am trying to write a bash shell script in Mac OS X 10.6 (which is called by an Automator Action) to rename TV show DVD rips that I have named badly over the years. I want to remove part of the text ...
2
votes
1answer
851 views

Need help converting a string from a file into a date

I'm trying to read a string from a file and then convert it into a date but for some reason it's failing. I'm actually modifying a GeekTool Geeklet that's not working at the moment. It reads a file ...
7
votes
4answers
1k views

How do I find the overlap of two strings in bash?

I have two strings. For the sake of the example they are set like this: string1="test toast" string2="test test" What I want is to find the overlap starting at the beginning of the strings. With ...
5
votes
2answers
2k views

Utility to get binary representation from string?

Is there a simple utility like printf that'll give me a "binary representation", i.e. a string like "01010010", from an input string? It would need to support multi-byte encodings.
2
votes
3answers
992 views

Is there a tr for translating into longer strings?

tr is great for translating a set of characters to another set of characters. Sometimes, I want to translate a set of characters to a set of strings, e.g. all < and > to &lt; and &gt;. ...
2
votes
3answers
739 views

Collapse repeating characters

Is there a more Bashist way than echo "$PWD//" | sed -e 's#//\+#/#g' to replace repeating slashes with a single one? Thanks Steven D, now there's a fully working function to find the longest common ...
2
votes
4answers
21k views

how to concatenate strings in bash?

I need to concatenate 2 strings in bash, so that: string1=hello string2=world mystring=string1+string2 echo mystring should produce helloworld
7
votes
7answers
5k views

How to check if $PWD is a subdirectory of a given path

E.g. check if $PWD is a subdirectory of /home. In other words I'm searching for a bash string operation to check if one string starts with another.
2
votes
1answer
926 views

Array of string expanded to path?

Suppose I have the following initialization of bash array: my_array=( "/usr/bin" "/usr/lib/*.so" ) If I do iteration using: for array_item in ${my_array[@]} do ... done Then the ...