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

learn more… | top users | synonyms

0
votes
1answer
22 views

Remove last n characters from -print0?

Currently I have: @find . -type f -name "img*_01.png" -print0 | python script.py -f {} is there a way to trim the last few chars like this? @find . -type f -name "img*_01.png" -print0 | python ...
1
vote
2answers
38 views

Condition Inside String in Bashscript

I want to print exit code of previous command in shell if it failed. So I changed PS1 variable in /etc/bash.bashrc like this: PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w [$?] \$ ' #...
1
vote
3answers
3k views

Extract the base file name from a URL using bash

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?
3
votes
1answer
30 views

Awk and discarding non-matching tokens in a string?

I'm having trouble getting a string tool to provide what I need (due to my ignorance). I have a string based on CPU features. The string will change as different processors offer different features: #...
4
votes
2answers
41 views

Bash - Integer expression expected

I am making the check for update script for my theme I have 2 text files. First one is called "current.txt" and contains the current version. There is 4.1.1 string in that text file. Second one is ...
-1
votes
1answer
1k views

How create calculator in linux script?

I am trying to create a calculator: echo "What is your number?" read n1 echo "what is your second number?" read n2 echo "what do you want to do?" echo "1. add" echo "2. subtract" echo "3. divide" ...
59
votes
6answers
101k views

How can I test if a variable is empty or contains only spaces?

The following bash syntax verifies if param isn't empty: [[ ! -z $param ]] For example: param="" [[ ! -z $param ]] && echo "I am not zero" No output and its fine. But when param ...
18
votes
4answers
117k views

how to concatenate string variables into a third?

I need to concatenate two strings in bash, so that: string1=hello string2=world mystring=string1+string2 echo mystring should produce helloworld
1
vote
2answers
33 views

using gsub in awk to replace a word with parentheses

Hi how can I use gsub to replace a word which has parentheses. Here I want to replace ABC(T) with ABC/G awk ' {gsub("ABC\(T\)","ABC/G")}; Print $0' "$FILENAME" > tmp.tmp && mv tmp.tmp "$...
0
votes
1answer
59 views

How to compare two variables containing ipv6 addresses?

I have to compare two variables with ipv6 so there are strings and integers together. I tried it as below: if [[ "$ipvsrc" -eq "$ipvdst" ]]; then echo -e "Value OK"; fi error output was: ...
1
vote
3answers
75 views

Extract filename from another filename

I receive notification files for different files. I want to extract the filename from these notification files which I would use in another process. The first 4 strings of the notification file will ...
1
vote
2answers
83 views

Perl stripping substring with $ in front

I get that perl thinks the part of the string with $ are perl variables. What I'm trying to do with the perl call, is redefine variables preceded by a $ as \$, but that's throwing other errors. I ...
1
vote
2answers
48 views

How to move string from one line to another

I have been editing a large text file containing pairs of lines as follows (repeated): \> destination_file.txt js -e "var e='.jpg',t='b',i='14712583',h='0.us.is.example.com',s='/',n='WIV',u='...
0
votes
1answer
48 views

copy lines of text between two files using string matches

Looking for some help, I have two files, one is a large list of various names the other is coordinates. both files start each line with an 8 digit code. I want to lookup 8 digit line code from File1 ...
-1
votes
1answer
39 views

Extract string from first character till string pattern

I have a implicit file with **$&*#@** as field delimiter and **$#@&*** as record delimiter. Below is an example, 100$&*#@200$&*#@50$&*#@Some String$&*#@2016-01-01 101010$#@&...
2
votes
1answer
21 views

Strings from two distinct arrays not matching?

I'm trying to filter one array from another array. That is, I'm trying to create a third array with a logical not-intersection. The best I can tell, it appears this block of code never matches, and ...
1
vote
1answer
47 views

completely ignore lines that start with a specific pattern

I have a script which analyzes the output of a computation software. sometimes the output comes with some extra data that are irrelevant to my script. This data can be huge and makes running my simple ...
1
vote
3answers
40 views

How to delete whitespace surrounding equal signs in a string?

I'm trying to determine a way to delete leading and trailing whitespace surrounding the equal sign from a string. The whitespace may (or may not) be present. There may be 0, 1 or more equal signs. I'...
2
votes
1answer
36 views

How can I use sed to snip a line off of a string?

I'm writing a script to display the domain registrar info and I'm trying to snip off a few lines from what I already have I currently have it grabbing all the indented text from a whois. Now I'm ...
11
votes
3answers
774 views

How do you remove dot character from string without calling sed or awk again?

I have a file called hostlist.txt that contains text like this: host1.mydomain.com host2.mydomain.com anotherhost www.mydomain.com login.mydomain.com somehost host3.mydomain.com I have the ...
2
votes
4answers
80 views

String Manipulation Shell Script [duplicate]

I am working on a UPS Monitoring project using NUT Server. My objective is to make a shell script which sends one command and in response receives the status and other parameters from the UPS. for ...
0
votes
1answer
103 views

Find string in text file and execute a script depending of the result

I have a file (Data.txt) with something like this: A=1234597890 B=192.168.1.1 sometimes B goes first and sometimes only A or B. So, If I find: A, Print A content and execute A script B, ...
1
vote
2answers
57 views

How can I add a filter to my grep script to NOT include a string?

I'm working on a script that will separate the Registrar information from a domains whois. So far it's working enough but there are a few things that I want to remove in order for it to be a bit ...
1
vote
4answers
187 views

Extract substring with character instead of position

I recently got into nautilus scripts, and for the one I'm writing I'd need to extract a substrong from a filename. My problem is that I found tons of methods to extract a substring based on the ...
1
vote
3answers
48 views

How can I use string manipulation to detect and remove two different parts of a string?

I'm making a script that will remove the http:// or https:// from a URL. So far I've got it to remove the http://, but I can't seem to figure out how to modify it so that it will remove either that ...
0
votes
1answer
63 views

How to use the 'date' command to display week number of the year?

I want the UNIX date to output: So this is week 35 of 2016. Here, of course, 35 and 2016 are outputs of the date command. I have tried the following: date +%U This printed out the current week ...
4
votes
5answers
4k views

Add leading characters in front of string using printf or echo

How can I add leading characters to fill a string to a certain length? Assume I want to add zeroes in front of a string, if said string is shorter than 20 characters: printf '%020s\n' "$line" ...
61
votes
11answers
131k views

Delete the last character of a string using string manipulation in shell script

I would like to delete the last character of a string, I tried this little script : #! /bin/sh t="lkj" t=${t:-2} echo $t but it prints "lkj", what I am doing wrong? Thank you in advance!
4
votes
2answers
3k views

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

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 ...
0
votes
1answer
58 views

grep with piping and showing multiple lines

I am trying to use grep to search a large text file, and return the paragraph containing a few key words. I also want to return the surrounding lines in the result. So, for example I have the ...
1
vote
4answers
59 views

Using cut to return the middle of a string

I have an xml string echoed to stdout along the lines of <xml:attribute>{41c33a-4893b-3627a-617a}</xml:attribute> I want to return the string {41c33a-4893b-3627a-617a} in this case. I ...
3
votes
4answers
194 views

Check if a shell variable has more than one line using built-ins?

I can do this by calling the external utility sed (for a known non-empty $myvar) like so: if [ "$(printf %s "$myvar" | sed -n '$=')" -eq 1 ]; then echo "Your variable has only one line, proceeding" ...
3
votes
5answers
165 views

Bash while loop read from colon-delimited list of paths using IFS

I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given ...
0
votes
1answer
26 views

assign string components into variables [duplicate]

Looking for a smarter way to do something I'm doing already. I have a string: "firstbit.secondbit.thirdbit.fourthbit.fifthbit.sixthbit" The content between the periods changes, but the delimited ...
6
votes
5answers
1k views

Replace string after last dot in bash

I have the following pattern in a string (an IP address): 123.444.888.235 I want to replace the last number after the dot with 0, so it becomes: 123.444.888.0 How could I do it in bash or ...
0
votes
1answer
79 views

ZSH, concatenate passed in arguments into a single string

I would like to create a simple bash function to use for my convenience. Following the answer given at: Joining bash arguments into single string with spaces I've been able to mash up this small piece ...
2
votes
0answers
46 views

put string to commandline

I am making a bashfunction that show a menu binded to pgdown to browse through a a window which list my history (like 4dos/4nt etcetera). I have made so one line is always highlighted. $pos is ...
11
votes
7answers
38k 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?
4
votes
3answers
125 views

Remove substring from front and back of variable

I know that I can remove substrings from the front of a variable: X=foobarbaz echo ${X#foo} # barbaz ...and from the back of a variable: echo ${X%baz} # foobar How do I combine the ...
1
vote
3answers
310 views

How to break a long string into multiple lines in the prompt of read -p within the source code?

I am writing an installation script that will be run as /bin/sh. There is a line prompting for a file: read -p "goat can try change directory if cd fails to do so. Would you like to add this feature?...
1
vote
1answer
43 views

Bash: How to get a value from an array to create command from it

Is it possible to use bash to fetch a string from an array provided in the following syntax within a config file? | | Version | PackageManager | Webserver | Database | |--------|---------|-----...
1
vote
4answers
82 views

Bash part of string without using chars in quotes

So I want to get from a string like: q='"Something, variable", another part, third one' The part "Something, variable". I could get '"Something' using ${q%%,*}. But how could I let bash ignore ...
1
vote
2answers
26 views

How prepend appropriate amount of 0's to numbers contained in image names?

I've got a folder /User/me/Desktop/folder/images with files like key 1--name.jpg, key 10--name1.jpg, etc. The files are sorted incorrectly when fed to my site so I get an arrangement like key 84--name....
17
votes
7answers
12k 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.
0
votes
0answers
26 views

Problem matching strings in bash script

I`m making a script to rename files in a folder. The new name for the files is in a config file. I have to be sure that the file I want to rename corresponds to the current one in the config file. My ...
1
vote
3answers
52 views

Get directory name from file name

I want to make a directory name by extracting parts of a file name. Here are some examples: server.log.2016-03-20-14 → 20160320 server-2016-03-17-13-16-Restart.log → 20160317 I'm using HP-UX.
1
vote
3answers
49 views

How to get a substring based on index of another string

I need to extract the integer value from some log files. The integer value always appears after a substring return code. But it may or may not have additional stuff after this substring. Below are two ...
3
votes
1answer
70 views

Rewrite arguments before passing them to a command

I use rtorrent. When using magnetic-links, it creates a "meta"-file (.meta). This gets the form of a long hexadecimal-number (0-9, A-F). For example: 0123456789ABCDEF0123456.meta To "use" an ...
3
votes
3answers
48 views

Strings - Extract File Version Recursive

I have been using strings to extract and compare file versions between directories. I don't seem to be able to do this recursively. The method I have been using per file is: strings -e l ...
2
votes
5answers
117 views

how to cut fields of a directory name?

I have directory named 140905_M01771_0112_000000000-ABBYT. I want to get the third field i.e. 0112. I tried: cut -d _ -f 3 131230_M01771_0032_000000000-A7874 giving me error: cut: ...