The tag has no wiki summary.

learn more… | top users | synonyms

10
votes
5answers
793 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 ...
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 ...
7
votes
3answers
436 views

How to remove a specific character in a string but only if there are no numbers in that line in Linux

I've been stumped with this seemingly simple-to-fix problem.. well, for a while. Here's example output from the file I need to edit: $cat file George Washington Geneva Convention 123,281,029 USD ...
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?
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 ...
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.
6
votes
6answers
350 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 ...
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 ...
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 ...
5
votes
3answers
3k 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 ...
5
votes
6answers
200 views

How to match exact string using `sed`? But not the part of it.?

I have a input file FILE1.TXT as below. 11 id1 12 13 AGE = 20 14 NAME = NAME1 15 16 id2 17 18 AGE = 30 19 NAME = NAME2 . . . 110 idXYZ 111 112 AGE = AGEXYZ 113 NAME = ...
5
votes
2answers
964 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 ...
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 ...
5
votes
2answers
101 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 ...
4
votes
2answers
1k 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.
4
votes
4answers
695 views

Replace whole line in a file from command-line

I have a text file which has some contents similar to this: # General information about the project. project = u'Py6S' copyright = u'2012, Robin Wilson' # The version info for the project you're ...
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
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
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 ...
3
votes
2answers
2k 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 ...
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 ...
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?
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 ...
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 ...
3
votes
3answers
45 views

string substitutions: all occurrences

A simple question, but I can't find the answer. the zsh documentation overwhelms me :-( function z () { echo ${1:s/the/THE} } $ z thethethe THEthethe ... How can I get all 'the' to be replaced ...
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"(?<=.)(?=(?:...)+$)", ",", ...
3
votes
2answers
348 views

Change string occurrences in file while respecting DOS new line sequences

I use Visual Studio for my C# development while using Cygwin for some of the tasks that require scripting. Recently I wanted to change all occurrences of string AAA to BBB in my project's files. I ...
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" | ...
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 ...
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
2
votes
5answers
290 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
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. ...
2
votes
5answers
560 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 ...
2
votes
2answers
834 views

Testing a string containing only spaces (tabs, or “ ”)?

My code below doesn't work: stringZ=" " if [[ "$stringZ" == ^[[:blank:]][[:blank:]]*$ ]];then echo string is blank else echo string is not blank fi Result: string is not blank # wrong ...
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
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 ...
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 ...
2
votes
4answers
389 views

Can awk use field identifiers also for shell strings (variables)?

Well, this apparently is not possible the way I'm trying it. This alternate approach to obtain bar as a resulting string works, though: #!/bin/bash path1=/usr/tmp/foo/bar/baz awk -F/ '{print $5}' ...
2
votes
3answers
437 views

Find word in sentence with Bourne Shell (sh)

I'm trying to determine if a particular word appears in a sentence using Bourne Shell (sh). For example: #!/bin/bash INPUT='Lorem ipsum dolor sit amet.' if [[ "$INPUT" == *dolor* ]] then echo ...
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 ...
2
votes
4answers
91 views

Compare two text files and find matching lines

I have two files A and B. A looks like this (4 to 6 lines): GAGA CAGA GGGT TATT file B is a really big file with thousands of lines, here is a short example: AAATGTCAAGAGACAGAAATGTCAAGAGGGT ...
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 ...
2
votes
2answers
767 views

How to remove error messages when searching recursively for a string?

I use find -type f -print0 | xargs -0 -n 10 grep -i searchstring to search recursively for a string. But it also gives me error messages like "permission denied". How can I avoid such error messages ...
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 ...
2
votes
1answer
38 views

How to print strings matching a pattern with grep?

I want to get a list of strings matching the following pattern: CLASS_NAME:"*" where * represents any number of characters. I tried: grep -o CLASS_NAME:\".*\" script.js and grep -o ...
2
votes
2answers
143 views

How to find and replace a field column value in UNIX

I have file with the value say which is delimited with | 123-|aaa|bbb|123|123.0|123-|123.01-|-123.02|123.03-|aaa|bbb|123-|aaa-|-bbb|cc-cc|123.04-|aa123-|123.05- I need to pick the column values ...
2
votes
3answers
60 views

Change value of a strings representing number

I have the following expression : a="2013" How can I change the value of a to "2012".
2
votes
1answer
122 views

shell parameter substitution to rename files

I need to rename filenames which starts with "foo" into "boo" This is the script I used #!/bin/sh for f in *.jpg; do mv -- "{$f}" "${f/foo/boo}"; done but when i run I get a bad substitution ...
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 ...
2
votes
2answers
400 views

remove only specific text occurrences from string using sed

I have a text file that contains many rows of this sort of thing: /*[17:51:27][1 ms]*/ UPDATE `country` SET `region_id` = '4' WHERE `country_id` = '36'; Is there a way that I can use sed to remove ...