A problem for sed? Remove parts of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A problem for sed? Remove parts of a string
# 1  
Old 09-08-2008
A problem for sed? Remove parts of a string

Hi,
My knowledge about sed is limited but I have a problem that I think can be solved with sed.

I have a variable in a shell script that stores a lot of path/filenames and the delimitter between them is a space (they all exist on the same line). One part of the filename is the file creation date.
Below an example of how a the variable store three instances of path/filenames:

/logdir/logfile_2008:08:12_Comp.Z /logdir/logfile_2008:08:13_Comp.Z
/logdir/logfile_2008:08:14_Comp.Z

I want to remove all path/filenames that doesnīt match an user specified interval of dates (handled earlier in the script). For example, maybe I have choosed the start date to 2008:08:14. That means that the instances with the dates 2008:08:12 and 2008:08:13 should be removed from the variable and the path/filename with the date 2008:08:14 should be kept in the variable.

How may this be solved with sed (or some other unix tool)?

Thanks,
Rick
# 2  
Old 09-08-2008
So you have a variable containing space-separated tokens and you want to discard some of them? If you are processing them one at a time anyway, perhaps you want something like this:

Code:
for file in $variable; do
  # Skip rest of loop unless it matches 2008:08:14
  case $file in *2008:08:14*) : keep going ;;  *) continue;; esac
  : do whatever you want to do with $file
done

If you just want to collect the values into the variable, "do whatever you want to do" amounts to copying the matching values to a new variable, and assigning it back to the main variable after "done".
# 3  
Old 09-08-2008
Hi,
I have noticed that it is all to slow to loop though the whole variable in a shell script. There may be hundreds or thousands path/filenames in the variable. Thatīs why I hope it is possible to solve with a tool like sed.

Thanks,
Rick
# 4  
Old 09-08-2008
Quote:
Originally Posted by pcrs
How may this be solved with sed (or some other unix tool)?

Use awk:

Code:
var='/logdir/logfile_2008:08:12_Comp.Z \
/logdir/logfile_2008:08:13_Comp.Z \
/logdir/logfile_2008:08:14_Comp.Z \
/logdir/logfile_2008:09:01_Comp.Z \
/logdir/logfile_2008:09:14_Comp.Z'

start=20080814
end=20080901
newvar=$(
printf "%s\n" $var |
 awk -F_ -v start=$start -v end=$end '
  {
   date = $2
   gsub( /:/,"",date )
  }
 date >= start && date <= end { printf "%s ", $0 }
'
)

# 5  
Old 09-09-2008
It worked perfectly.
Thanks cfajohnson.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed remove everything between two string

Hi all, I have this input: "203324780",,"89321213261247090146","VfdsD150","0D","fd3221","V0343","aaa","Direkt","fsa","2015.02.27","39833,54454,21214",,,"fd","NORMAL","D","10fd","1243 Flotta","HiĂĄnytalan","2013.02.25",,"2013.02.25","2013.02.24","2013.02.28",,"SajĂĄt... (4 Replies)
Discussion started by: snayper
4 Replies

2. Shell Programming and Scripting

sed awk to remove the , in a string

Dear All, Can anyone help to remove the , bewteen "" in a string by using sed or awk? e.g. input : 1,4,5,"abcdef","we,are,here",4,"help hep" output:1,4,5,"abcdef","wearehere",4,"help hep" Thanks, Mimi (5 Replies)
Discussion started by: mimilaw
5 Replies

3. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

4. Shell Programming and Scripting

Remove duplicate chars and sort string [SED]

Hi, INPUT: DCBADD OUTPUT: ABCD The SED script should alphabetically sort the chars in the string and remove the duplicate chars. (5 Replies)
Discussion started by: jds93
5 Replies

5. Shell Programming and Scripting

Remove parts from a filename

I want to remove the beginning and end of a filename and keep the middle. E.g. tempblast7114_1#21110932.out_ the current filename I want it to be called 7114_1#21 only How would I do this?? (3 Replies)
Discussion started by: avonm
3 Replies

6. Shell Programming and Scripting

Sed is doing my head in! How do you remove the first character of a string?

Hello! Please bare with me, I'm a total newbie to scripting. Here's the sudo code of what I'm trying to do: Get file name Does file exist? If true get length of file name get network id (this will be the last 3 numbers of the file name) loop x 2 If... (1 Reply)
Discussion started by: KatieV
1 Replies

7. Shell Programming and Scripting

use sed to remove year from string?

Hi guys, I have been trying to play with sed to accomplish this but I just can't quite get it right. I need to be able to remove the year from a string held in a variable in my bash script. The string may have multiple words but always ends with a year such as (2009) for example: ... (2 Replies)
Discussion started by: tret
2 Replies

8. Shell Programming and Scripting

How to remove all matches in a string with sed

if I have "abxcdxefx" and want to remove the x's with sed, how can I do this? Thanks. WHOOPS: Just remembered: echo "abxcdxefx" | sed s/x//g Thanks for reading, though. (0 Replies)
Discussion started by: lumix
0 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question