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

learn more… | top users | synonyms

1
vote
3answers
117 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 ...
1
vote
0answers
64 views

Automatic replacement of strings in gedit

I want to replace some strings defined by me in gedit, after a word has been completed and/or the document has been saved. For example \ðrac should be replaced with \frac How do I do this in gedit or ...
0
votes
2answers
137 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
178 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
583 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}' ...
0
votes
2answers
2k views

Extract a sub-string out of a variable's value

I have a string in the form of segment_78787 which is stored in a variable called $ID in my shell script. I need to get the second part "78787" and assign it to another variable. I tried like ...
2
votes
1answer
87 views

strings command printing some extra characters

I am trying to capture mysql traffic using tcpdump and converting it to text using strings command, but while capturing mysql traffic, I am getting some extra character at the end of each character. I ...
2
votes
1answer
307 views

IPTables string match redirection

The issue: I am currently redirecting port 80 requests to another system on port 1000. This is done like so: iptables -t nat -A PREROUTING ! -s 172.20.1.2 -p tcp --dport 80 -j DNAT --to-destination ...
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
0
votes
2answers
284 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, ...
2
votes
1answer
177 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
1k 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 ...
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?
0
votes
3answers
283 views

Extract the base file name from a URL

url=http://www.foo.bar/file.ext; echo ${url##/*} I expected this code to print file.ext, but it prints the whole URL. Why? How can I extract the file name?
6
votes
2answers
4k 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
998 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
1k 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 ...
1
vote
2answers
2k views

Pattern matching from the input arguments

we're trying to enhance the scripts. Users will pass some arguments and part of the arguments will have 5.0.3 For an example the input argument would be like Jboss5.0.3GA. Since it ( Jboss5.0.3GA ) ...
2
votes
2answers
158 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
125 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
770 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
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 ...
2
votes
3answers
530 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
3answers
434 views

How to reverse-match a string in the Vim programming language?

I want to find the last index of any character in the [abc] set in the abcabc string but the search should start from the end of the string: " Returns the 0th index but I want the 5th. let ...
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 ...
6
votes
4answers
23k 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
1answer
5k 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
5k 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 ...
2
votes
3answers
512 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 ...
0
votes
1answer
139 views

awk extended pattern matching (embedding pattern matching in actions for already matched strings) [closed]

I want handle strings of the form: PREFIX_TYPE_N,DATA So, does the *awk (gawk, mawk, nawk) support including pattern matching in the action for already matched string? Something like this (of ...
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
1answer
662 views

How to capitalize word (i.e. first letter to upper) in KornShell

Is there a builtin way in KornShell to capitalize a word, e.g. korn -> Korn? A Bash 4 example to clarify: str='korn' echo "${str^}" If there is not a bultin way to do this in KornShell, what is the ...
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 -> ...
2
votes
2answers
492 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 ...
3
votes
2answers
415 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 ...
2
votes
2answers
979 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 ...
0
votes
1answer
117 views

du (disk usage) and format specifier for file/directory sizes strings?

I am really fond of du, and I often like to use it like this: $ du -hsc /var/mail/ /var/log/ 2>/dev/null 4,0K /var/mail/ 5,7M /var/log/ 5,7M total However, I'd like to be able to ...
5
votes
2answers
1k 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 ...
4
votes
4answers
824 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 ...
1
vote
1answer
2k 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
792 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
241 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 ...
1
vote
2answers
512 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 ...
8
votes
3answers
8k 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
446 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
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 ...
2
votes
4answers
287 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
231 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?