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

learn more… | top users | synonyms

1
vote
0answers
17 views

Search for the multiple string and print it to file in order [migrated]

I have a data as below, and I need to filter out the Author with "file changes and Lines Modified/Added Lines Deleted" And I need to separately print it to file with respect to Authors. If there are ...
2
votes
4answers
162 views

Extracting the second word from a string variable

I have a string "rtcpOnNbActive true" stored in a variable x. I want to extract "true" as substring and store in a variable. How can I do this?
1
vote
6answers
122 views

How to convert dashed string to spaces and caps?

Given the string foo-bar, what is a good way to convert it to Foo Bar? Using Bash, OSX.
0
votes
2answers
47 views

Bash regex string manipulation bug

I have an issue with bash and its regex match ability that I for now think is a bug in bash but could also be me having missed something obvious. I hope it is the latter. I have made this function in ...
0
votes
1answer
37 views

Issue finding lines using grep in UNIX [closed]

I am trying to use a .txt file with around 5000 patterns (spaced with a line) to search through another file of 18000 lines for any matches. So far I've tried every form of grep and awk I can find on ...
3
votes
4answers
280 views

Command to remove a portion of JSON data from each line?

I have test.json file with different lengths of rows. Some fictitious example: { a: 123, b: sd, c: x45, d: 1, e: '' } { a: 5, b: bfgg, c: x4c, d: 31, e: '' } I want to cut the whole substring after ...
4
votes
3answers
160 views

Change case of n-th letter in a string

I want to change the case of the n-th letter of a string in BASH (or any other *nix tools, e.g. sed, awk, tr, etc). I know that you can change the case a whole string using: ${str,,} # to lowercase ...
11
votes
3answers
352 views

Get all possible combinations of a word in lower/uppercase letters

I want to write a bash script to print all possible lower and upper case permutations of a certain word, e.g. harley: harley harleY harlEy harLey ... HARLey HARLEy HARLEY My naive solution is to ...
1
vote
3answers
115 views

Print total free memory from `free -m`

free -m currently puts out something like this. I would like to know using grep/awk how we can get the total free amount. i.e. 9083
1
vote
5answers
104 views

How to create an array of unique elements from a string/array in bash?

If I have a string "1 2 3 2 1" - or an array [1,2,3,2,1] - how can I select the unique values, i.e. "1 2 3 2 1" produces "1 2 3" or [1,2,3,2,1] produces [1,2,3] Similar to uniq but uniq seems ...
1
vote
1answer
35 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, ...
3
votes
7answers
227 views

get parts of string using shell script

I have one string as /ip/192.168.0.1/port/8080/ I want to get two separate variables which will contain port and IP like. 192.168.0.1 and 8080 as I know /ip/ and /port/ will be there always I ...
4
votes
4answers
281 views

How do I show the numeric character sequence of a string?

Is there a command either standalone or in vi or similar (can be gui) that will show me the numeric sequence of a string? Input: The cat hopped in a box. Output: T h e c a t h o p p e d ...
6
votes
2answers
220 views

Piping bash string manipulation

I've read some other piping bash string manipulation questions but they seem to be specialized applications. Essentially, is there a way to do the below simpler? instead of $ string='hello world'; ...
3
votes
2answers
177 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
319 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
32 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
91 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
457 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
297 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
181 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
184 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
261 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
74 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 ...
0
votes
1answer
69 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
91 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
2k 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 ...
1
vote
1answer
58 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
128 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
183 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
56 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
7k 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
148 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
159 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
365 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
250 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
73 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
109 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 ...
6
votes
6answers
11k 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
199 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
62 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
267 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
302 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
161 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
739 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
41 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
258 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 ...
3
votes
3answers
2k 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
3k 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
11k 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: ...