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

learn more… | top users | synonyms

6
votes
3answers
2k views

How to defer variable expansion

I was wanting to initialize some strings at the top of my script with variables that have no yet been set, such as: str1='I went to ${PLACE} and saw ${EVENT}' str2='If you do ${ACTION} you will ...
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 ...
3
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
4answers
410 views

Parsing process command-line arguments from pargs in a shell script

I get list of PIDs in my bash scripts (Java processes) and have to analyze their command-line arguments to determine which instance of server each PID corresponds to. At the moment I do it that way ...
2
votes
5answers
3k views

Extract date from a variable in a different format

Let me explain you the problem $ date +%c -d "$d" Tue 31 Dec 2013 01:13:06 PM CET $ date +'Today is %F' -d "$d" Today is 2013-12-31 This solution corresponds to current date. But I have one ...
1
vote
4answers
514 views

Stripping all vowels but the first from a set of strings

I have a string comprised of multiple substrings, separated by underscores. For example: AbcdAEfd_hEgdgE_AbAAAAA. I need to remove all vowels except the first from each substring. So: AbcdAEfd -> ...
3
votes
3answers
8k views

How to get the first word of a string?

When I 'echo *' I get the following output: file1 file2 file3 ... What I want is to pick out the first word. How can I proceed?
2
votes
4answers
17k 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
6
votes
4answers
24k views

Splitting string by the first occurrence of a delimiter

I have a string in the next format id;some text here with possible ; inside and want to split it to 2 strings by first occurrence of the ;. So, it should be: id and some text here with possible ; ...
4
votes
2answers
434 views

Convert MAC address to Link-local address with bash

How can I convert a Mac address into an ipv6 Link-Local address? you have to add fe80:: at the start and insert ff:fe in the middle furthermore all leading zeros must be stripped
2
votes
2answers
91 views

Filtering the Xth element in a row?

For example, I want to get only the 3rd element in each row when I call: xinput --list --short|grep "slave pointer" I get the output: ⎜ ↳ Virtual core XTEST pointer id=4 ...
1
vote
3answers
161 views

Script to split string

@terdon's answer provides this script: find . -type f -name "*mp3" | while read file; do album="$(basename "$(dirname "$file")")"; filename="$(basename "$file")"; ...
1
vote
4answers
1k views

Appending a string containing escape character with sed

Currently I use: sed -i -e "5a\\ ${text}" $filename to append something to a certain line, where the variable text contains a string such as "\epsilon". When using echo -E $text the string is ...
1
vote
2answers
521 views

Checking if PATH contains $HOME/mydir and adding it if not (all in a script) [duplicate]

Possible Duplicate: keep duplicates out of $PATH on source I am not an expert with Unix scripting. Some of my scripts are located in $HOME/mydir. Unfortunately, they are not accessible from ...