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

learn more… | top users | synonyms

8
votes
6answers
366 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 ...
0
votes
3answers
53 views

grep substring between quotes

Let's say I have a string like this: title="2010-09-11 11:22:45Z" How can I grep the date itself and disregard the quotes/title/Z? The file can contain more strings like: randomstring ...
4
votes
4answers
111 views

Check if content of file has length X and contains only specific characters

Let's say, I have files that contain only one docker id: myid.id: 28fe2baadbe8da32ed0b99c69b11c01b2d141bc5b732b81e0960086de52fc891 I want to check if the content of my.id is exactly 64 characters ...
4
votes
2answers
66 views

Replace very long string in files recursively

I have a very long and complex string in many files and I want to remove/replace it recursively. The string contains many slashes, backslashes and spaces and any kind of special signs. How do I do ...
4
votes
5answers
89 views

Add leading characters infront 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 infront of a string, if said string is shorter than 20 characters: printf '%020s\n' "$line" ...
0
votes
1answer
28 views

Must pass urls in quotes

I'm trying pass a URL to mpv for it to play it as a network stream. This can be done in bash with the following syntax: $ mpv http://myvideosite.com However, zsh wants to evaluate the URL as ...
1
vote
1answer
53 views

Verify the length of a variable

I've to verify the length of variable read (my script limit to five the characters inserted), I think about something like this: #!/bin/bash read string check=${#string} echo $check if [ $check -ge 5 ...
4
votes
5answers
238 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!
0
votes
0answers
13 views

Java: Parsing the JSON file [migrated]

I have a Json file like this {"Name":"Saaa","AppIcon":"ddd.jpg","Wallpaper.jpg","ddd.jpg"]} I need to extract the AppIcon values. I'm using json simple lib to parse the json. The code snippet to ...
5
votes
1answer
163 views

Test -n gives unexpected result

I understand test -n <expression> to return false if the expression evaluates to a string length of greater than 0. Why then does the following happen? Macbook:~ echo ${#undeclared_variable} 0 ...
0
votes
3answers
27 views

Extract line number of a file which is having a non zero value before a specified string

I'm having a file which contains following data 1. verification: 10 passed 0 failed 2. verification: 10 passed 0 failed 3. verification: 10 passed 1 failed 4. verification: 10 passed 3 failed 5. ...
3
votes
3answers
160 views

Replace multiple strings in a single pass

I'm looking for a way to replace placeholder strings in a template file with concrete values, with common Unix tools (bash, sed, awk, maybe perl). It is important that the replacement is done in a ...
0
votes
5answers
102 views

how to extract part of a filename before '.' or before extension

I have files in format below: abc_asdfjhdsf_dfksfj_12345678.csv hjjhk_hkjh_asd_asd_sd_98765498.csv hgh_nn_25342134.exe I want to get the value before the . and after the last _. The result would ...
0
votes
4answers
59 views

grep to see if a line ends with a particular string

Hello I have a file that has a lot of lines that end with main.css">, and some of them are repeated. I want to keep only one copy of these lines and delete the repeated line. Could I use grep to ...
1
vote
3answers
150 views

How to get a string between two special characters using Shell?

I want to get a string between two special characters. For example, /home/oracle/ggs/text.ext I want oracle/ggs in the above string. How can I do this using bash?
0
votes
2answers
27 views

Trim last chars from a line

I have a csv file and the lines have different length. But all of them ends with a number surrounded by a pair of braces. For example, ........{5} ........{16} I'd like to remove the number and the ...
4
votes
5answers
187 views

Replace new line + 4x space to new line in stdout

I need join 2 lines into one in stdout - replace new line AND 4x space with one space but new line without space don't touch tcpdump -vvv ... -l | xyz this is output: 2014-06-06 AAA BBB ...
1
vote
3answers
241 views

How to capture error message from executed command?

I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and ...
3
votes
3answers
302 views

Case-insensitive substring search in a shell script [closed]

I'm trying to create a script which runs a command and compares the output of that command to a certain string to see if it contains that substring, ignoring the case of the string needed to be ...
5
votes
6answers
2k 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
426 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
294 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
215 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
160 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
52 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
101 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
50 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
3k 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
432 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
6answers
901 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
442 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
1k 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
722 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
283 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
594 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
249 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
50 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
162 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
66 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
380 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
68 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
58 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
81 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
3k 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
252 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
4k 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
87 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
74 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
54 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, ...