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

learn more… | top users | synonyms

1
vote
4answers
73 views

How to get only one character after match in string (to determine RHEL version from rpm)

I have an open source shell/bash script for setting up and configuring hosting servers, and to install cloudflare I am pulling the version of RHEL using this command: RHEL_VERSION=$(rpm -q --qf ...
1
vote
1answer
135 views

Remove leading 0 from specific field

I have a file with multiple fields but only want to remove the leading 0s from the 2nd column (col B) rather than all of the columns. I have tried: sed 's/^0//g' filename > new_filename but this ...
1
vote
1answer
96 views

How to extract date and calculate if it is within “x” days from today?

Currently I have file with structure as Foo Sign: blah SubFoo Sign: blah BarDate: 2017-11-31 Foo Sign: blah BarDate: Never Foo Sign: blah BarDate: 2016-12-20 Foo Sign: blah ...
0
votes
1answer
51 views

Raspbian: evaluate conditional strings and/or numbers

I am having a really a hard time to evaluate conditional strings and numbers. After hours of reading and trying with (my Beginners Guide) I keep getting the error msgs like syntax error in conditional ...
1
vote
2answers
79 views

Use “find” from a specific relative directory [closed]

Suppose I'm in the following directory: /A-dir/B-dir/mydir/… some directories …/x-dir I want find to search from mydir, but mydir is not specifically known in advance; it is defined by the current ...
1
vote
2answers
82 views

How to replace values in a string using sed but keep the string intact

How to replace values in a string using sed but keep the string intact? For example echo "01XX1234" I'm looking to replace XX with a hex value and leave the 01 and the 1234 intact. I've tried every ...
6
votes
5answers
629 views

How to abbreviate /path/to/file to /p/t/file

I'm looking for an elegant one-liner (eg, awk) that will shorten a string of a Unix path using the first character of each parent/intermediate level, but the full basename. Easier to show by ...
1
vote
3answers
57 views

How to extract “foo” from “foo-bar-baz” in bash? [duplicate]

I'm trying to extract foo from the string "foo-bar-baz" in bash. Any ideas how to do it using string substitution or something similar (without external tools). I tried this but it didn't work: $ ...
2
votes
3answers
78 views

List variables with prefix where the prefix is stored in another variable

I'm trying to list all the variables with a certain prefix, but that prefix is dynamically determined. For example: prefix=apple_ apple_one=a1 apple_two=a2 If I simply do: echo ${!apple_@} I ...
1
vote
1answer
37 views

date: Getting next certain hour

I want to get a date-time string in the future, for example I always want to get the next time of 9:00 am. I know the string formats like "tomorrow" or "next day" to get a certain date-time string. ...
3
votes
1answer
40 views

Removing substrings from a shell variable

I have some string that read like this one here Dio - We Rock-Greatest Hits (2CD) (2004) I use this NewDirectoryName=${NewDirectoryName%'(" ")'* } NewDirectoryName=${NewDirectoryName//[^A-Za-z ]/ ...
1
vote
4answers
2k views

Bash: remove numbers (strip numeric characters) from string variable

I want to strip all numeric characters from a String variable, eg from: VARIABLE=qwe123rty567 to: echo $VARIABLE > qwerty I've search many posts but they either use sed to output to file/file ...
1
vote
1answer
223 views

Find and delete lines containing multiple patterns in a file

I have a list of strings and want to find and delete lines containing these strings in a file. A short example of the list of strings is listed as below. File S 1Mo 32,33 2Mo 7,26 2Mo 7,27 2Mo 7,28 ...
0
votes
1answer
48 views

if statement not working properly

I have a loop comparing strings that it still does not kick it out if it matches. the set up is many different types of file extensions, only want to delete the ones I do not want keeping the ones I ...
0
votes
2answers
55 views

How to remove non english chars from a string

I have a BASH script I wrote that removes META data off mp3 (etc) then I chop them up to reform them if needed resample the mp3 and reasign the data to make a directory to corraspond to artist / album ...
2
votes
2answers
219 views

How to escape single or double quotes when passing a variable to bash script?

Let;s say I have a script which echoes $1 #!/bin/bash echo $1 It's called test.sh. Then I call /bin/test.sh 'test'. The output is test. But this doesnt work: /bin/test.sh 'te'st' There's a ...
1
vote
3answers
100 views

How to check for valid $PATH directories, and output them telling whether each one is valid or not?

Ok so I got this: check="${PATH//:/'\n'}" The above gets me each path in a simplified manner, and formats it nicely into a variable. Now how do I check each line for existence, and state that for ...
1
vote
3answers
135 views

Match lines beginning with the same pattern in two text files

I am now having two text files with some of the lines beginning with the same pattern. For example, File A 1Mo 1,1 I love you. 1Mo 1,2 I like you. Hi 23,3 I am hungry. Hi 23,4 I am foolish. File ...
1
vote
3answers
49 views

Bash How can I wrap text at a certain number of space separated inputs

I have a long string like: "1 2 6 9 18 19 25 67 89 102 140 187" and I want to fold or wrap it after a specified number of inputs or spaces using (e.g. fold on the third input) rather an the number of ...
2
votes
1answer
85 views

Bash String Expansion Corrupt

I have the following: echo "PLATFORM = $PLATFORM" DATE_STRING=`date +"20%y-%m-%d"` echo "DATE_STRING = $DATE_STRING" # VERSION_LINE will be something like: '#define VERSION_STRING "1.2p2"' ...
1
vote
1answer
31 views

How can I access a column of a line from within a string?

I have a file I'm traversing in a for loop. I want to do something like... for line in file.out; do echo "Some text before $line($2) $line($3)"; done Where, for every line I insert the 2nd and 3rd ...
0
votes
1answer
46 views

Expanding a brace expansion string held in a variable for use in a for loop?

As a complete newbie to bash, I've got the following script: FOOS=foo{1..5} for i in `echo $FOOS` do echo bar done Now I (think I) get the idea that the brace expansion only works with literals - ...
0
votes
1answer
41 views

Can you replace displayed text with something else in command window?

I am trying to manage user accounts. They are basically just numbers, but I would rather see user last names on the screen. Is it possible to automatically replace displayed text with something else? ...
1
vote
2answers
60 views

How do I stream edit progress-like output?

I'm having trouble editing some progress output. In this case it's Postgres' pg_basebackup, but it's similar to other progress output like curl and wget. Take for example the output of this: ...
2
votes
2answers
97 views

Remove specific (known) extension from filename if it's there

I'm not even sure whether grep is meant to be used for regex in one-line data. Googling led me to this lookahead expression that, along with grep, matches any .ext file and returns bare name: ...
-4
votes
2answers
46 views

A script that can check subdiractories for a file that contains 'hello' and gives back directory name

i need a script where you give a directory name as a parameter and the script check all the subdirectories .php files and if any file contains a string 'Hello' then the script list the matches files ...
1
vote
2answers
37 views

extract part of the text from all matching lines

I am running a for loop against set of servers to find the cert expiry. Output of the cert command will be below for each server: line 1 line 2 line 3 Valid From: Tuesday, 29 September 2015 10:40:13 ...
2
votes
2answers
91 views

Cleaning PHP exploits from infected files using sed

Recently my server has been a target for a series php exploit attacks and using ClamAV I was able to identify many infected files. The catch here is that there are legitimate files that are infected ...
4
votes
2answers
65 views

How to use the results of “file” (Name of Creating Application: Microsoft Word) to search for a specific string?

I am trying use the results of "file" which returns info about the file/s I specify. E.g. file *.doc 'all .doc extensions This then returns information about the file including "Name of Creating ...
0
votes
0answers
24 views

How to reference a variable by its string “name”? [duplicate]

Say for example... #!/bin/bash a1=one a2=two a3=three for x in {1..3}; { echo -n "\$a$x "; } Actual.. $a1 $a2 $a3 Expected.. one two three How can one echo, reference, call, or what ...
2
votes
2answers
89 views

Identify sub-directories that do not contain a specific string in a specific file

I have a directory named dir1 which has about 800 sub-directories named disp-001, disp-002, ... disp-800. I need to find sub-directories that either do not contain a file stdout or if they do, that ...
27
votes
8answers
8k views

How to generate a random string?

I would like to generate a random string (e.g. passwords, user names, etc.). It should be possible to specify the needed length (e.g. 13 chars). What tools can I use? (For security and privacy ...
3
votes
3answers
69 views

How do I cut a string already separated by spaces?

Lets say there's a string like this test="1/2/3 4/5/6 7/8/9/0" They are separated by spaces, as well as '/'. I want to return a result like this by taking the second field of each string segment. ...
2
votes
1answer
61 views

python Vs bash string slicing

My file a contains the text, bcd\\\\. With bash, i read the file and print characters from 4th to 8th position as, tmp=$(cat a) echo "${tmp:3:4}" It prints, \\\\ All happy. Now i use ...
18
votes
2answers
2k views

Is there something like JavaScript's “split()” in the shell?

It's very easy to use split() in JavaScript to break a string into an array. What about shell script? Say I want to do this: $ script.sh var1_var2_var3 When the user give such string ...
0
votes
1answer
196 views

RRDtool update not working when integers and floats are mixed

I'm trying to store ADSL stats for my router. I didn't do rrdtool info on the others beccause they are now showing the correct value after using the same command as in the script to update. The weird ...
1
vote
1answer
59 views

Storing integer values from string in rrd

I want to collect stats from my DSL modem every 10 minutes. According to lots of websites it's best to use rrd for this. My modem(TD-W8968) struggles with SNMP so I've made an expect script to pull ...
0
votes
1answer
49 views

Issues with storing an echo of a date conversion into a string variable inunix

Apologies but this question comes from a question previously raised: How can I convert a local date-time into UTC date-time? Toby has been absolutely great helping me with some conversion string-->BST ...
0
votes
1answer
42 views

replacing first link of a text “awk equivalent of sed command”

I wanted to replace the first line of a text with sed it should be like sed -i.bak "1 s/^.*$/"CompoundState\tMethod\tApproach\tS^2\tEnergy\tPath"/" awk.xls but I also need to run it on OSX but it ...
0
votes
0answers
44 views

Subtract a Number from variable which contains a sentence

This is extended question from http://stackoverflow.com/questions/32081168/reading-a-log-file-with-different-sequences-using-shell-script I'm new to shell. I've written a shell script a code with ...
0
votes
0answers
76 views

Concatenating two strings cuts string

I extract two numerical strings from a textfile and and want to combine them to create a new string. The new string will be a filename. This is the textfile dates.dat: 378464,2015-01-31 ...
0
votes
1answer
92 views

Grep all strings from several binary files into one txt file

I've decided to play around dumping all process memory of couple utilities in order to find out, how much trace of sensitive info an attacker could theoretically obtain from app's memory. I've found ...
0
votes
1answer
110 views

Find part of String and replace entire string in a file

I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2". I want to find and replace all the strings in the file starting with Ticket_ with something called ...
-1
votes
1answer
86 views

Comparing folder name to line from text file

I'm attempting to compare the name of a directory to a line in a text file. However even when the name of the directory and the line in the text file are the same I can't seem to get them to be ...
1
vote
5answers
162 views

How to get all numbers out of a string and add them?

I have to parse a .xml file generated to summarize the results of a running a testSuite on some software. In a line I have, for example: <Summary failed="10" notExecuted="0" timeout="0" ...
8
votes
4answers
1k views

How to assert that a string has a newline character and, if so, remove it

I have a string that is the result of some operation I have no control over. When I print this variable using echo, I get: echo $myvar hello However, when I do if [ $myvar = "hello" ]; then ...
5
votes
3answers
290 views

Go from a String to an Array of “words” in Bash

I need to go from a string to an array where each entry is each word on that string. For example, starting with: VotePedro="Vote for Pedro" I need the array: Vote For Pedro Which I should then ...
0
votes
1answer
30 views

Remove Ubuntu strings from Mini install

I have an assignment to remove a string from Ubuntu Mini v. 15.04. How would I remove the Ubuntu String on the top? It says "Ubuntu 15.04 hostname tty1". I just want to remove Ubuntu 15.04 string. ...
0
votes
1answer
24 views

Substituting the value with spaces for a variable in bash script - the correct way

I have a compilation script that can compile the program with and without the debug symbols. I want to ask via bash prompt ( read ) the user to choose whether he wants a debug version or not with ...
2
votes
3answers
106 views

Find strings in file1, count occurrences in file2

I have file1.txt with string values such as New Drug Application Drug Product Dosing instructions I need to count how often these strings occur in file2.txt with data such as Regulatory New Drug ...