Delete Last part


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete Last part
# 1  
Old 03-28-2012
Delete Last part

Hello,

I am on solaris (ksh). I have a log file like this with directory structure and file name :

Code:
/DIR1/DIR2/DIR3/filename
/DIR1/DIR2/filename
/DIR1/DIR2/DIR3/DIR4/DIR5/filename

I am looking for a way to create a new file to be formated like this :

Code:
DIR2/DIR3
DIR2
DIR2/DIR3/DIR4/DIR5

The idea is to :
1 : get rid of the first directory name and the file name listed inside of the last directory in the log file

2 : and then to be able to create a loop in a shell script to re-create - on a different way - the directory sctructure on another disk.

Tanks in advance for your help
# 2  
Old 03-28-2012
bash

Hi,

Try this one,

Code:
#! /usr/bin/bash
while read line
do
line=${line%\/*}
line=${line#\/*\/}
echo $line >>file.new
done <file

Look at the sed and awk solutions too Smilie

Code:
awk '{gsub(/\/[^/]*$|^\/[^/]*/,"");}1' file >newfile
sed 's/\/[^/]*$//;s/^\/[^/]*//g' file>newfile

Cheers,
RangaSmilie

Last edited by rangarasan; 03-28-2012 at 10:19 AM..
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 03-28-2012
A sed one:
Code:
$ sed "s|/[^/]*/\(.*\)/.*|\1|" file
DIR2/DIR3
DIR2
DIR2/DIR3/DIR4/DIR5

This User Gave Thanks to Scott For This Post:
# 4  
Old 03-28-2012
To get rid of dir1 and filename:

Code:
sed "s:/[^/]*/::; s:/[^/]*$::" inputfile


--------------------------------------------------
Woh! Already answered by two members!

Last edited by balajesuri; 03-28-2012 at 10:15 AM.. Reason: Late post!
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 03-28-2012
Wait, how about one more.... ;-)

Code:
while read line;do echo $line | sed 's/^\/....\/\(.*\)\/.*/\1/g' >> file-new;done < file

# 6  
Old 03-28-2012
Thank you so much for your help .... You were so fast

Dear Rangarasan,

your command

Code:
awk '{gsub(/\/[^/]*$|^\/[^/]*/,"");}1'

gives me error :

Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

maybe it's because I am on ksh, but I did try with "nawk" but no success ... Thanks anyway for your help


Thanks In2nix4life, but you code give no change ... strange ...
# 7  
Old 03-28-2012
awk

Thats works fine in my GNU awk Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

To Delete the duplicates using Part of File Name

I am using the below script to delete duplicate files but it is not working for directories with more than 10k files "Argument is too long" is getting for ls -t. Tried to replace ls -t with find . -type f \( -iname "*.xml" \) -printf '%T@ %p\n' | sort -rg | sed -r 's/* //' | awk... (8 Replies)
Discussion started by: gold2k8
8 Replies

3. Shell Programming and Scripting

Delete part of a column

I want to delete a part of the 4th column from the given file below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (2 Replies)
Discussion started by: my_Perl
2 Replies

4. Shell Programming and Scripting

Find and delete part of field with awk or sed

I've got a file that looks like this (the whitespace between commas is intentional): 123456789,12,JOHN H DOE ,DOE/JOHN H ,,,DOE/JOHN H ,,,,,123 FAKE STREET ,SPRINGFIELD,XX, I want to strip just the first name out of the third field so it reads "JOHN,". So far I... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

delete only part of a line

Hi, I've a file as below- I need to delete only the part of the line after the pattern 'extent /lock' and my output has to be like below. Thanks (7 Replies)
Discussion started by: dvah
7 Replies

7. Shell Programming and Scripting

delete part of file from pattern1 to pattern2

Hi all! How can I delete all the text starting from <string1> to <string2> in all the .txt files of the folder "FOLDER" ? Thanks a lot! mjomba ... </s> <s> <w></w> </s> <s> ... to get: (1 Reply)
Discussion started by: mjomba
1 Replies

8. Shell Programming and Scripting

Delete part of string

This command: du -s /Applications/TextMate.app Returns an output like this: 65792 /Applications/TextMate.app I need to delete the space and the file path in the output leaving just the number. Thanks (2 Replies)
Discussion started by: pcwiz
2 Replies

9. Shell Programming and Scripting

comparing part of header with part of detailed records.

Hi there, I am lil confused with the following issue. I have a File, which has the following header: IMSHRATE_043008_101016 a sample detailed record is :9820101 A982005000CAVG030108000000000000010169000MAR 2008 9820102 MAR 2008 D030108 ... (1 Reply)
Discussion started by: cmaroju
1 Replies

10. Shell Programming and Scripting

How to delete part of a file?

Suppose i have a file which contains 10 lines.... i have to delete a line which starts with the word "BEGIN" and delete the consecutive lines till i find a start of line with the word "END" how to do this? (6 Replies)
Discussion started by: brkavi_in
6 Replies
Login or Register to Ask a Question