The string tag has no wiki summary.
5
votes
6answers
253 views
How to split such a string into array in bash
I have problem with output of a program. I need to launch a command in bash and take its ouput (string) and split it to add new lines in certain places. The string looks like this:
battery.charge: ...
2
votes
1answer
80 views
C Shell script condition for if string contains a newline character
I'm not sure how to express this using the csh string matching syntax. I want to test whether a csh variable contains a newline. I'm basically looking for:
if ($mystr !~ <pattern for strings which ...
2
votes
2answers
116 views
How to add a string with spaces at the end of command output
I want to add a string with spaces at the end of each line of the output of ls command.
#!/bin/sh
STRING_WITH_SPACES="this should be at the end of output"
ls -lh | sed 's|$| '$STRING_WITH_SPACES'|'
...
3
votes
1answer
190 views
What does the hash in ${parameter/#pattern/string} do?
I saw the following substitution in this article:
${PWD/#$HOME/~}
How does it compare to this?
${PWD/$HOME/~}
Both seem to be the same. I don't know why the hash was included.
2
votes
2answers
74 views
Creating an array with IFS set to a different value
I am trying to construct an array and write to a file in this format, i.e. the file's content should be something like this
hero_pairscore=( askdjfh sdf,sdlkfj lksf,dfgdf,dsflkgj,asdlkf ....)
Where ...
2
votes
2answers
44 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 ...
2
votes
1answer
43 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 ...
0
votes
3answers
37 views
How to find the difference in tokens in two strings using Unix tools?
I have two strings as below-
token1, token2, token3, token4, token5, token6, token8, token9, token10
token2, token7, token4, token3, token5, token6, token8, token10, token9
Visually, I can see ...
6
votes
6answers
550 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
2answers
174 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
5answers
348 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
125 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
4answers
128 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
...
7
votes
3answers
467 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
...
1
vote
3answers
173 views
How to loop a read function in Bash Script?
I needed to remove spaces and capital letters to various strings of text like:
"My Name is Mauro" -> "my_name_is_mauro"
So I have created this bash script:lowercase_underscore.sh (call it the ...
5
votes
6answers
232 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
117 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 ...
3
votes
3answers
47 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 ...
1
vote
3answers
92 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
1answer
37 views
Is there a simple way to get array of all arguments that do not begin with a hyphen?
zsh includes a powerful utility for parsing command-line options zparseopts, but for quick shell scripts and shell functions, but is there an easy way to extract the array of all command-line ...
3
votes
2answers
136 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"(?<=.)(?=(?:...)+$)", ",", ...
2
votes
3answers
62 views
Sed - string substitution with groupings
I have a largish directory with filenames formatted like
Some_Folder-(FOL001)-clean
what I'm trying to do is display the pattern between the brackets at the start like
FOL001 ...
0
votes
1answer
35 views
create an alias that runs a command with string replacement from arguments [duplicate]
I'm trying to write a couple of shortcut aliases to quickly navigate to the httpdocs folders of the various domains and subdomains on my server. basically so I can type something like:
$ pwd
/
$ # ...
1
vote
3answers
50 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
...
2
votes
5answers
912 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 ...
0
votes
0answers
15 views
UNIX: How to extract date from a variable storing a UNIX DATE [duplicate]
I am facing following issue:
Input:
F=`date +%Y%m%d`
echo $F
Output:
F=20131231
But when trying this(File Name:A.sh):
C=`date`
G=`$C +%Y%m%d`
echo $C
echo $G
Output:
A.sh[6]: Tue: not ...
1
vote
1answer
161 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
2k 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
76 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 ...
2
votes
3answers
65 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".
1
vote
1answer
43 views
Looping through commands' results, KSH script
I'd like to put two strings in two different variables.
Let's take a small example.
after a simple grep, I got this :
$ grep nl.*acc.*bas :
ABAS01=...
ABAS02=...
I'd like to have two variables, ...
1
vote
3answers
78 views
a string representation using regex (modified)
I would like to use regular expression to represent a pattern with the following type of characteristics
1) It includes a continuous sequence of digit values. The length of this sequence it at least ...
0
votes
1answer
187 views
Test variable if its string or not
I'm writing a script which will have some arguments and so I am using getopts
but i want to solve the problem with one argument.
I use a switch, for example -d, and I want the argument for -d will ...
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
87 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
50 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
130 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
169 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
411 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
817 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
73 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
248 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
317 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
225 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
128 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
963 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
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?
0
votes
3answers
248 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?
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
775 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. ...