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

learn more… | top users | synonyms

3
votes
3answers
97 views

Shell test to find a pattern in a string

I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path. if [[ $line =~ ...
0
votes
0answers
49 views

Replace a string with another string in a text file [on hold]

I want to replace a string that is in the file file1 with another string. I tried sed -i -e "s/chaines1/chaine2/g" file1 but the problem is I can't pass the two strings on the command line ...
1
vote
2answers
30 views

Read file remove spaces and store in array

I have a file with the following content: list: blue,none red,none orange,plot baseball , none university,none school,none desk,plot monitor,none earphone,none I need to read this file, ...
2
votes
2answers
31 views

How to transform string with character codes into quoted form

I have a string like New\x20Folder. Special characters is represented by their codes. I'd like to transform the string into quoted form: "New Folder". The only available tools are bash and bunch of ...
0
votes
2answers
39 views

how to split the string after and before the space in shell script?

I am having a variable which shows on echo like this $ echo $var 129 148 I have to take only 129 as output. How will I split 129 and 148?
1
vote
2answers
21 views

Substituting file extensions on a variable

I'd like to do the following : create a .tex and directly show it after compilation with evince, ideally it would be like this: showpdf file.tex so I wrote #!/bin/sh pdflatex $1 && ...
2
votes
3answers
91 views

Comparing strings

I am having trouble comparing two strings. Even though they are identical when printed, my if statement returns false. #!/bin/bash string1="HDMI" string2="PC Speaker" hash=$(pacmd list-sinks | ...
9
votes
1answer
204 views

When can I edit strings in an executable binary?

I have an executable binary; let's call it a.out. I can see the binary contains strings $ strings a.out ... /usr/share/foo .... I need to change the string /usr/share/foo to /usr/share/bar. Can I ...
0
votes
3answers
44 views

How can I capture the common characters between two strings

I'm looking to compare two strings and capture the common part e.g. FILE0000010985.LOG FILE0000010999.LOG FILE0000011000.LOG If I compare 1 to 2, I want the output to be ...
2
votes
2answers
73 views

How to extract the positive numbers in the first column from an output as in the question?

I am running Ubuntu 14.04.1 LTS 64-bit with Bash 4.3.11(1)-release I have a program called harminv producing output as follows: $ h5totxt hsli0.126.h5 | harminv -vt 0.1 -w 2-3 -a 0.9 -f 200 # ...
0
votes
1answer
72 views

Reading a string till a key word and replacing from there with another string

I am new to UNIX and am practising shell scripting. I have an application where I need string searching and replacement. There are some lines in a file 1 and file 2. I have a search key word. If the ...
1
vote
2answers
43 views

Why quotes are retained in string variables when surrounded by single quotes?

I needed to retain the double quotes around a string variable defined in bash in order to be able to pass it to a dialect of the Scheme programming language. My question is why are the double quotes ...
1
vote
2answers
73 views

How to extract a part of file name in unix/linux shell script

My file name is Oracle.2347263_testing_152607.csv I tried this : echo Oracle.2347263_testing_152607.csv | sed -e 's/.*G.//' \-e 's/.csv//' but I didn't get the result I expected. I want to ...
-4
votes
2answers
56 views

how to find a file extension with multiple dots [duplicate]

I'd like to extract the extension out of a filename with multiple dots. For instance: gbamidi-v1.0.tar.gz I should get "tar.gz", not "gz" or "0.tar.gz". I prefer a solution not relying on bash ...
1
vote
5answers
469 views

How to use grep and cut in script to obtain website URLs from an HTML file

I am trying to use grep and cut to extract URLs from an HTML file. The links look like: <a href="http://examplewebsite.com/"> Other websites have .net, .gov, but I assume I could make the cut ...
0
votes
4answers
150 views

How do i change the case (upper and lower case) of strings in my variable?

"Enter test: " read test if [[ $test == "a" ]]; then echo "worked" else echo "failed" fi It's a simple illustration of test i'm doing, but if i enter "A", it will fail, is there anything i ...
1
vote
1answer
58 views

script to parse integer and convert it to char code

I am noob on Unix. I've a very unique problem where and really appreciate your help. I need to parse string containing hash code value and convert the hash code is equivalent char representation. Here ...
2
votes
3answers
139 views

How can I insert the contents of a file into a string in bash

I would like to append the contents of a multi-line text file after a particular line in a string. For example, if the file file.txt contains line 1 line 2 I'd like do so something like printf ...
2
votes
0answers
65 views

Escaping strings in associative arrays (bash)

I am writing a script which executes a number of programs with arguments. To simplify it as much as possible, consider the following example: programs=( "ls" "echo" ) declare -A parameters ...
0
votes
1answer
29 views

How do i create a function to test if the input contains any character? [duplicate]

I have this set of example here to test if the user has keyed in any data after he presses enter. echo -n "Type a digit or a letter > " read character blank="" if [ "$character" != "$blank"]; ...
-1
votes
2answers
38 views

extract line from same string

I have a table like this: and a file containing 2 string from the third column as: apple potato i would like to extract the header and all the line that contain the string apple and potato to ...
1
vote
2answers
46 views

Modifying part of a string in bash script

I have a file named variables.f90 , having many lines defining different variables, as follows :: integer::n_monomer=6800 real*8::rx=5.0d0 #... randomly integer and real numbers defined ...
3
votes
8answers
202 views

bc output binary as nibbles separated by whitespace

I'm debugging code which contains quite a few bit shift operations, and I'm using bc a lot to look at what's happening on the bit level. Here's what I use: $ echo 'obase=2;598980975283696640' | bc ...
2
votes
0answers
41 views

Generate array of strings combined with number

I would like to generate a local array in a shell/bash script that consists of the following elements: JF-1998, JF-1999, JF-2000,... , JF-2011. That is, a string JF- combined with the years from 1998 ...
0
votes
2answers
48 views

Copying two words from a string into two variables

I want to get the variables $color and $number from a string that in general is like this: "something, numColor (number)". The color might be W, U, B, R, G. If there is no color the variable color ...
2
votes
1answer
67 views

Why string comparision results always false

I am trying to do a one time check about apache's mod-status page for updates like this(this is just a test script): firstcontent=$(lynx -dump http://some-server/server-status-page) echo ...
2
votes
4answers
564 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
139 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
114 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
52 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
355 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
282 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
659 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 ...
2
votes
3answers
165 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
379 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
40 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
605 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
299 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
381 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
324 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
2k 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
39 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
141 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
910 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
452 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
863 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
362 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
317 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
96 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
74 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 ...