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

learn more… | top users | synonyms

2
votes
2answers
96 views

Shell script to assign values from a lookup table is too slow

I'm trying to assign some variables from a lookup file with a shell script. I have something working but it seems unnecessary slow. Script: while read line do ...
3
votes
2answers
258 views

grep on a variable

Lets say I have a variable line="This is where we select from a table." now I want to grep how many times does select occur in the sentence. grep -ci "select" $line I tried that, but it did not ...
-1
votes
1answer
25 views

What is the easiest way to count the amount of letters in a string entered

I am currently writing a program in the raspberry pi that will allow a user to enter a string, and have the program count the amount of letters in it. I am looking into expr but I don't know how to ...
1
vote
2answers
67 views

(SOLVED) How I get the current working directory with inverted slash?

I'm trying to write a script to import a font in wine, because I put the font in wine's c:\Windows\Fonts, but wine doesn't read it if I don't import from the reg file: "Font ...
4
votes
4answers
275 views

extract every nth character from a string

I am trying to figure out a solution for this question. My approach to this problem so far is as below. Append all the characters together to make it a long string. After the above step, remove ...
6
votes
5answers
255 views

Number of characters in a shell command's output

I am writing a script which needs to calculate the number of characters in a command's output in a single step. For example, using the command readlink -f /etc/fstab should return 10 because the ...
2
votes
2answers
67 views

JSON parsing on the shell

How can I parse JSON output on the shell? For example, Amazon Web Services provides a CLI to retrieve the status of your instances: $ aws ec2 describe-instances <my_instance_id> But the ...
1
vote
3answers
131 views

keep only certain part of a string in a certain column

I have a file like this: id target_id length eff_length 1 intron_FBgn0000721:20_FBgn0000721:18 1136 243.944268 1 intron_FBgn0000721:19_FBgn0000721:18 1122 ...
1
vote
4answers
234 views

How to split a line by multiple characters?

Output of w | head -1 can give 2 different results for my machine: 16:34:42 up 32 days, 3:02, 1 user, load average: 0.14, 0.11, 0.07 14:10:55 up 32 days, 39 min, 1 user, load average: 0.07, 0.13, ...
4
votes
1answer
63 views

Length of argument inside a function of an sh script

I have a FreeBSD 9.3 RELEASE server and I am trying to write a shell script using sh. I am trying to get the length of a variable that is passed to a function in a shell script, like so: #!/bin/sh ...
-2
votes
0answers
52 views

How to create a string

I have the following naming conventions, I would like from all of them, only to convert those having the name in form IMG_????.JPG and exclude the rest. -rw-r--r-- 1 highpassion highpassion 2651 ...
0
votes
1answer
51 views

Finding words with one vowel group

I am trying to use a command to find all words in a list of words with one group vowels. Like four, for and far, but also words like ore and or. What I got so far: grep -i ...
0
votes
3answers
62 views

Bash Combine Replacement and Sub String Extraction in One Step

For a string in this format: ./my-site.sub.domain.com I would like to remove the .,-, and /, and trim the remainder to 16 characters: mysitesubdomainc I've done this ...
5
votes
7answers
709 views

How to find a position of a character using grep?

I need to identify the postion of a character in string using grep command. Example, the string is RAMSITALSKHMAN|1223333. grep -n '[^a-zA-Z0-9\$\~\%\#\^]' How do I find the position of | in the ...
0
votes
1answer
36 views

Zsh script how to concatanate array elements with string

I have written a zsh script to automize an analysis in high energy physics and now I would like to use an element of the one of the defined array some string and another element of some another array ...
4
votes
2answers
96 views

Write variable containing large text with \n to a file with common shell interpreters. How do you do it?

Environment: linux shell or better, ash shell in busybox (example system: Openwrt). Given a string variable with large text "${a}" constructed in this way: for index in $(seq 1 40000); do #it is ...
3
votes
2answers
146 views

Script using “split” in a loop over large files

I'm attempting to get a bash script working, and running into terrible errors along the way. The aim of the script is to break several large text files, into multiple files with a line count read from ...
0
votes
3answers
54 views

String index processing

My input file looks like: #key string pos(string) key1 AA000AA000000AAA0A 2, 3, 18, 12 key2 00A00AAA000AAAA00A 3, 18 And I'd like to add a new column at ...
9
votes
6answers
3k 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
93 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
148 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
137 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
153 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
48 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
64 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 ...
5
votes
5answers
4k 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!
5
votes
1answer
183 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
40 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. ...
5
votes
3answers
207 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
147 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
110 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
605 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
35 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
219 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 ...
2
votes
3answers
942 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 ...
4
votes
3answers
2k 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
8k 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
1k 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
779 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
235 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
375 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
60 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
252 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
58 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
5k 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
770 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 ...
3
votes
6answers
2k 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
1k 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 ...
3
votes
4answers
3k 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
1k 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 ...