Variable string manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable string manipulation
# 1  
Old 04-28-2013
Variable string manipulation

Hi, I have a variable with grep output like this:
Code:
WORDS=$(grep -r -c -i -E "palindrom" /"$DIRECTORY"/)

so "echo "$WORDS"" could be:
Code:
//directory/file1.txt:0
//directory/file2.txt:0
//directory/file3.txt:3
//directory/file4.txt:1
//directory/file5.txt:0

I need to "sed" my variable "$WORDS" in order to extract only strings with ":x" with x!=0, i.e. in this case I only want to print:
Code:
//directory/file3.txt:3
//directory/file4.txt:1

Thanks
# 2  
Old 04-28-2013
Code:
WORDS=$(grep -r -c -i -E "palindrom" /"$DIRECTORY"/ | awk -F ':' '$2>0')

This uses awk....
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-28-2013
thanks it work perfectly.
# 4  
Old 04-28-2013
Here is another way, and this could possibly be fitted into your other grep:
Code:
WORDS=$(grep -r -c -i -E "palindrom" /"$DIRECTORY"/ | grep -v ":0$")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh String Manipulation - removing variables from within a variable

Hi. I'd like to remove all values in a string variable that also exist in a second variable. What is the appropriate approach to take here? I can use a 'For' loop and check each element and then populate a new string. But is there a cleaner, simpler way? E.g. I have the following 2 variables ... (19 Replies)
Discussion started by: user052009
19 Replies

2. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

3. Shell Programming and Scripting

String manipulation

I want to do the next "I don't want to go school because I'm sick today." I want to join these two line but only when the first line is not more than 20 characters and ended whit nothing or a comma and the second line not more than 15. The 20 and the 15 can be change in the script. I know... (10 Replies)
Discussion started by: thailand
10 Replies

4. Shell Programming and Scripting

String manipulation

Hi All, Pls help me out on the below, 05 LAMSZ201-ZM-MEMO2-DATE02-5 PIC X(10). 05 LAMSZ201-ZM-MEMO2-AMT02-5 PIC S9(13)V99. 05 LAMSZ201-ZM-MEMO2-TYPE02-6 PIC XXX. 05 LAMSZ201-ZM-MEMO2-DATE02-6 PIC X(10). 05 ... (2 Replies)
Discussion started by: baskivs
2 Replies

5. Homework & Coursework Questions

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. Homework... (0 Replies)
Discussion started by: shashwat2691
0 Replies

6. Shell Programming and Scripting

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. (1 Reply)
Discussion started by: shashwat2691
1 Replies

7. Shell Programming and Scripting

Bash string variable manipulation

In a bash script I've set a variable that is the directory name of where an executable lives. the_dir=`dirname $which myscript` which equates to something like "/path/to/dir/bin" I need to cut that down to remove the "bin" so I now have "/path/to/dir/". This sounds easy but as a... (2 Replies)
Discussion started by: Witty
2 Replies

8. Shell Programming and Scripting

string manipulation

Hi all, see i have a script that takes few arguments. first one is command we do on file, next is file (mostly txt file with lot of data) third is destination where we do something with data in file. Since im new in scripting, and im learning as i go, i need some hint how to manipulate that... (3 Replies)
Discussion started by: ajemrunner
3 Replies

9. UNIX for Dummies Questions & Answers

string manipulation

Hi, I have a file with rows of text like so : E100005568374098100000015667 D100005568374032000000112682 H100005228374060800000002430 I need to grab just the last digits(bolded) of each line without the proceeding text/numbers. Thanks (5 Replies)
Discussion started by: james6
5 Replies

10. Shell Programming and Scripting

variable manipulation

Coded in my script: #!/bin/ksh I have a variable in the format of YYYYMMDD: $latest_bus_date_yyyymmdd I need to create a new variable in the format of MMDDYYYY: $latest_bus_date_mmddyyyy I want to take the information from $latest_bus_date_yyyymmdd and reformat it to be stored in the new... (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question