2
votes
2answers
41 views

How can I select a beginning…ending portion of a '/' delimited string (PS1 prompt in my case)

I am customizing my PS1 prompt, specifically the $PWD part which is delimited by /'s among several things I like having the current directory. However if the directory is too long I'd like to have ...
6
votes
6answers
352 views

bash find lines starting with string

I have a bunch of files and I want to find which one contains sequential lines starting with a certain string. For example for the following file : Aaaaaaaaaaaa Baaaaaaaaaaa Cxxxxxxxxx Cyyyyyyyyy ...
2
votes
5answers
294 views

How to reverse a string made of digit in bash?

What is the best way to turn out digits in number? E.g. $ echo 123 | hook 321 $ echo 12358 | hook 85321
2
votes
4answers
104 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 ...
5
votes
2answers
102 views

ls for cut -f string not working

I have a problem with feeding cut output to ls. This doesn't work: rep="S1_1000,0000-00-00c,0000-00-00d,0000-00-00e" ls tab_yeast/{`echo $rep | cut -f2- -d','`}*.tab.gz ls: cannot access ...
1
vote
3answers
80 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")"; ...
3
votes
2answers
104 views

Add thousands separator in a number

In python re.sub(r"(?<=.)(?=(?:...)+$)", ",", stroke ) To split a number by triplets, e.g.: echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?<=.)(?=(?:...)+$)", ",", ...
1
vote
3answers
44 views

Match a single character in a string bash script

My issues is that I want to read a character from the stdin and compare it with a string value. If that character exists then I want to display a message. Eg, saving % into the variable $a: $ read a ...
1
vote
1answer
132 views

Problem with sed on a array containing strings containing spaces

I have a array looking like this: array=("(1 2 3) (123)" "2 31 (231)" "4 5 1 (451)" "(te)xt (1234)") This array is a example. It does not look like this but its structure is the same (the strings ...
2
votes
6answers
1k views

bash - replace space with new line

How can I replace spaces with new lines on an input like: /path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5 etc... To obtain the following: /path/to/file /path/to/file2 ...
5
votes
3answers
73 views

how do I set quotes around a variable so that the programs sees them as quote marks

I am trying to get quotes around a variable to make is just like I typed it in the terminal to get this script to work. it shows " quotes around the varibale " but still does not see it as quotes, as ...
1
vote
4answers
448 views

How do I delete files with spaces in them in bash script?

this is a three part topic to a two part process that still is not working for me: Objective: to manipulate a filename then delete it the filename has spaces between the words then the dot .ext ...
3
votes
2answers
1k views

Bash : compare two strings with space

I am trying to write a bash script which run a command and compare the result with another string. #!/bin/bash -x STATUS=`/root/setup_ha show --password-file=/root/password | grep ">HA State" | ...
1
vote
3answers
80 views

splitting the string in bash script

I am new to bash script. I am taking some input from user and checking details about that. Example: $HOME/Documents/test/one.txt I take the above string as an input and want to retrieve one.txt, I ...
0
votes
2answers
124 views

Transform list of files (with date) into list of dates

I have a directory where I store backups. The are in the format Complete Backup YYYY-MM-DD hh-mm-ss.tar.bz2 I have found out that you can feed a date into the date command using date --date="YY-MM-DD ...
3
votes
5answers
167 views

Comparing Indices of two strings

I have two double quoted strings of same length, that look like this: "$con" - (consists of spaces and *'s): * ****** *** ** * ** and ...
4
votes
2answers
293 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
0
votes
2answers
206 views

search fitting ipv6 address for MAC-Address in neighbours

I have a list of possible to ip6 addresses converted MAC-addresses, that could be found in the output of ping6 ff02::1%wlan0 possible mac addresses are for example: 66:70:02:75:80:fe, ...
3
votes
3answers
6k 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?
5
votes
2answers
3k views

Extracting a string, according to a pattern, in a bash script

In bash, suppose that I have a string strname: strname="ph7go04325r" I would like to extract the characters between the first "3" character and the last "r" character in strname, saving the result ...
2
votes
5answers
723 views

Test if a string has a period in it with bash

I want to run a bash command on output from Drupal's drush command-line interface. drush site-alias returns a list of webroots, first showing the name of the group, and then each site in that group. ...
10
votes
5answers
795 views

Pattern matching on path names in bash

I want to act on a list of subdirectories in a directory. Consider: for x in x86-headers/*/C/populate.sh; do echo $x; done This gives x86-headers/elf/C/populate.sh x86-headers/gl/C/populate.sh ...
2
votes
2answers
147 views

Bash variables and types

I'm making a script that validate an IP address. I do this: read pool checkIp() { local ip=$1 local stat=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then ...
0
votes
1answer
118 views

Alter path of find result [duplicate]

Possible Duplicate: Manipulate file name piped from find command How can I alter the path of a file found with find before I run an exec on it? I want to find files and then mv them to a ...
1
vote
2answers
512 views

How to correctly concatenate strings in shells script?

I have a bash shell which I need to modify, and I have to set a variable in a script and then call another script. My variable, EXTRA_JAVA_OPTIONS must be -javaagent:myagent.jar="-d 0 -i 1000 -l log2 ...
6
votes
3answers
1k 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 ...
2
votes
3answers
407 views

Search and replacing a string on specific file extensions

I have this bash: replace="s/AAAA/BBBB/g"; find myDirectory/. -type f -print0 | xargs -0 sed -i $replace; that will recursively scan myDirectory tree and replace all occurrences of AAAA with ...
3
votes
4answers
17k 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 ; ...
3
votes
1answer
4k views

bash - how to uppercase the command line argument?

I searched SO and found that to uppercase a string following would work str="Some string" echo ${str^^} But I tried to do a similar thing on a command-line argument, which gave me the following ...
1
vote
3answers
3k views

bash string to int

Here is what I tried doing: #!/bin/sh res=`df | awk 'FNR == 2 { print $5 }'` res2=$((res+0)) Here is my error: -bash: 53%: syntax error: operand expected (error token is "%") I Googled ...
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
4answers
479 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 -> ...
5
votes
2answers
967 views

Splitting bash command line argument

Is this the best way to split up a colon separated bash command line argument? #!/bin/bash hostlist=`echo $1| awk '{split($0,Ip,":")} END{for (var in Ip) print Ip[var];}'` for host in $hostlist do ...
1
vote
1answer
1k views

How to assign value of variable with regex (bash)?

I want to use regex in bash at line of variable assignment e.g. oldip="14\.130\.31\.172" oldip_a="14.130.31.172" << How to use regex to del all '\'? and assign to oldip_a Do you have any ...
3
votes
3answers
621 views

Extracting text from a text file in bash

I have a large text file that is all one line. In the text are several sections that looks like foo=12345 and I need to extract them all as separate lines, ignoring the rest of the file. For ...
1
vote
2answers
202 views

Variable name from command output

I have some string, and want to split on colon ":" assigning on variable with name from left part and value from right part. For example: echo "Title: Some title" | sed 's/:.*//' gives me wanted ...
8
votes
3answers
7k 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
4answers
276 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
1k 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
2k 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 ...
3
votes
3answers
2k 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
126 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
179 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
7k 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 ...
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 ...
2
votes
3answers
508 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
13k 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
7answers
4k 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
844 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 ...