scan and edit in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting scan and edit in bash
# 1  
Old 07-19-2012
scan and edit in bash

so assume I have a dozen files in local directory and half of them are .txt and I only want to scan these text files and go inside each of them and replace absolute paths (e.g. C:\blabla\more blahblah\myfile.txt) with just the name of that file (myfile.txt) and then go to next line and look if there are more paths to replace, and when done go to next text file. can someone please show me how to do this?
# 2  
Old 07-19-2012
Can you show how your txt look like?
I wanted to know if the "C:\blabla\more blahblah\myfile.txt" is on separate line or on same line with other words, etc.

considering they are on separate lines....here is a piece of code...not tested though

Code:
find <local directory> -type f -name "*.txt" > all_text_files.dat

while read FILE_NAME
do
    cat $FILE_NAME | grep "C:\" > absolute_paths.dat
    
    while read PATH_FILE
    do
        INSIDE_FL_NME=`basename $PATH_FILE`
        
        cat $FILE_NAME | sed "s|$PATH_FILE|$INSIDE_FL_NME|g" > temp_file.dat
        
        cp temp_file.dat $FILE_NAME
        
    done < absolute_paths.dat
    
done < all_text_files.dat

corrected the code in the cp statement

Last edited by aster007; 07-19-2012 at 01:20 PM..
# 3  
Old 07-19-2012
Quote:
Originally Posted by aster007
Can you show how your txt look like?
I wanted to know if the "C:\blabla\more blahblah\myfile.txt" is on seperate line or on same line with other words, etc.
file looks like this:

Code:
some line
comment1 C:\blabla\more blahblah\myfile1
comment2 C:\blabla\more blahblah\myfile2
some other line

and I need it to only show:

Code:
some line
comment1 myfile1
comment2 myfile2
some other line

---------- Post updated at 11:14 AM ---------- Previous update was at 10:57 AM ----------

Quote:
Originally Posted by aster007
here is a piece of code...not tested though
I get this error:
Code:
syntax error at line 19: `end of file' unexpected


Last edited by Scott; 07-19-2012 at 01:39 PM.. Reason: Code tags
# 4  
Old 07-19-2012
Quote:
Originally Posted by Jaymz
file looks like this:

some line
comment1 C:\blabla\more blahblah\myfile1
comment2 C:\blabla\more blahblah\myfile2
some other line

and I need it to only show:

some line
comment1 myfile1
comment2 myfile2
some other line

---------- Post updated at 11:14 AM ---------- Previous update was at 10:57 AM ----------



I get this error:
syntax error at line 19: `end of file' unexpected

corrected the cp statement.
Also, use this for getting the second part

Code:
cat $FILE_NAME | grep "C:\" | awk '{print $2}' > absolute_paths.dat

# 5  
Old 07-19-2012
I used both corrections and now I get:

Code:
line 11: unexpected EOF while looking for matching `"'
line 19: syntax error: unexpected end of file


Last edited by Scott; 07-19-2012 at 01:38 PM.. Reason: Code tags
# 6  
Old 07-19-2012
Quote:
Originally Posted by Jaymz
I used both corrections and now I get:

Code:
line 11: unexpected EOF while looking for matching `"'
line 19: syntax error: unexpected end of file

small details!! Smilie there was a "\" in the grep....must have been escaping the quote!
Code:
cat $FILE_NAME | grep "C:" | awk '{print $2}' > absolute_paths.dat

# 7  
Old 07-19-2012
ah yes! it works fine with that change Smilie
now it only needs to do the file editing I mentioned in the beginning and it's all done:
Code:
C:\blabla\more blahblah\myfile1

has to be replaced with:
Code:
myfile1

inside each line and each of the files.

---------- Post updated at 12:53 PM ---------- Previous update was at 12:24 PM ----------

I feel like maybe I didn't explain it clearly enough so sorry about that. All I needed is that the file content remains untouched except that absolute paths get reduced to just the file name they contain, which is now not happening as I see the original file being copied over as is. Thanks a lot for all your help so far!

---------- Post updated at 02:12 PM ---------- Previous update was at 12:53 PM ----------

I think sed is doing something wrong as it's not removing the path as it should.

---------- Post updated at 02:34 PM ---------- Previous update was at 02:12 PM ----------

nope it's not the sed it's the basename ! that line just copied the path as it is and then it pastes it over so obviously the final file remains unchanged Smilie help?

---------- Post updated at 03:03 PM ---------- Previous update was at 02:34 PM ----------

is it possible a unix/linux system does not interpret correctly Windows paths as in the examples I provided? that could be a cause nothing is changed from the original file. in this case how can I tell my unix system that the Windows path is really a path and not a regular string?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scan and remove if file infected using bash

The below bash runs clamav on all files in DIR and produces virus-scan.log. My question is the portion in bold is supposed to move the infected files, lines not OK, to /home/cmccabe/quarantine. Does the bash look correct? Thank you :). virus-scan.log Mon Jan 16 14:39:05 CST 2017... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Create automated scan of specific directory using bash

I am trying to use bash to automate the scan of a specific directory using clamav. Having this in place is a network requirement. The below is an attempt to: 1. count the extensions (.txt, .jpeg) in a directory and write them to a virus-scan.log (section in bold) 2. scan each folder in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated. My example: sed -i '14 i\ # add these lines add these lines to... (5 Replies)
Discussion started by: Nvizn
5 Replies

4. Shell Programming and Scripting

Plus edit and edit header

Dear Masters, i have a problem with unix script, till now i just know about how to create header. i want to change file below -63395.2 72653.5 -63361.3 72696.0 ... (9 Replies)
Discussion started by: ipatah
9 Replies

5. UNIX for Dummies Questions & Answers

best way to scan?

i want to scan all open and closed ports on a server. how can i do this. i intend on using nmap, but if there are better ways to do it, please let me know. i understand there are a total of 6335 allowable ports on a server. so out of that 6335, i want to know which is open or closed. id... (1 Reply)
Discussion started by: SkySmart
1 Replies

6. Shell Programming and Scripting

Edit csv file with bash

I' trying to use bash to edit a csv file from this format: "apples","oranges","grapes" "bread","butter","milk" To this: "apples oranges grapes" So that if I would open the csv file in open office, "apples oranges grapes" Would be in one single cell, and "bread (2 Replies)
Discussion started by: locoroco
2 Replies

7. UNIX Desktop Questions & Answers

grub, ok, bash, edit, kmdb and what else?

Hello There is a > prompt at Grub, # prompt for the console and $ for bash, but I am clueless about when and how to get into a specific prompt, how to move around between one prompt to another and how to exit. Is there a very basic guide anywhere that CLEARLY explains the type of shell... (2 Replies)
Discussion started by: mani1413
2 Replies

8. Shell Programming and Scripting

In bash, read to a variable with a default value to edit

My script needs to read a variable from the user. But before the user types the input, I want to give a default value so the user can edit the default value for input. How can I implement it? My script is something like: #!/bin/sh read -p 'Port number: ' -e port_number echo "Port... (7 Replies)
Discussion started by: pankai
7 Replies

9. Red Hat

how to edit the bash shell?

Hello friends, I want to add some features to the bash shell.I logged in as the root.Even then could not view the source code of bash shell in RH 9,Fedora 8.It is in encrypted form.Can you please tell me how to include my code into bash shell. Please give me a sample code so that I can understand... (3 Replies)
Discussion started by: nsharath
3 Replies

10. UNIX for Dummies Questions & Answers

IP Name scan

Hi. how to search a range of IP:s for their registed IP names? Like nslookup or host for all IPs 130.xxx.xxx.1 to 130.xxx.xxx.254 //nicke (2 Replies)
Discussion started by: nicke30
2 Replies
Login or Register to Ask a Question