File Parsing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Parsing
# 1  
Old 03-23-2016
File Parsing

Hi Gurus,

i have files like this and i want to rename it.

Code:
server1_0_Log0000597500
server1_0_Log0000597501
server1_0_Log0000597502
server1_0_Log0000597503
server1_0_Log0000597504
server1_0_Log0000597505
server1_0_Log0000597506
server1_0_Log0000597507
server1_0_Log0000597508
server1_0_Log0000597509
server1_0_Log0000597510

output should be like this:
Code:
mv server1_0_Log0000597500	 server2_0_Log0000597500
mv server1_0_Log0000597501	 server2_0_Log0000597501
mv server1_0_Log0000597502	 server2_0_Log0000597502
mv server1_0_Log0000597503	 server2_0_Log0000597503
mv server1_0_Log0000597504	 server2_0_Log0000597504
mv server1_0_Log0000597505	 server2_0_Log0000597505
mv server1_0_Log0000597506	 server2_0_Log0000597506
mv server1_0_Log0000597507	 server2_0_Log0000597507
mv server1_0_Log0000597508	 server2_0_Log0000597508
mv server1_0_Log0000597509	 server2_0_Log0000597509
mv server1_0_Log0000597510	 server2_0_Log0000597510



can you please help be on a script to do it?

thanks
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, sample output, and code segments.

Continued refusal to properly format you posts may result in you being banned from this site.

Last edited by Don Cragun; 03-23-2016 at 08:57 PM.. Reason: Add CODE tags again.
# 2  
Old 03-23-2016
What operating system are you using?

What shell are you using?

What have you tried to solve this problem?
# 3  
Old 03-24-2016
Hi fedora132010,

Assuming that you have a file.list with either the full path or you are executing the following command in the directory where these files exist.

Code:
perl -MFile::Copy=move -nle '$old = $_; s/server1/server2/ and move $old, $_' file.list

# 4  
Old 03-24-2016
Here is bash script to move those files and rename them

Code:
for i in $(ls); do mv $i ${i/${i:0:7}/server2}; done

It replaces the string between the 0th and the 7th character with "server2".
# 5  
Old 03-24-2016
Hi.

And a laundry list of items to consider:
Code:
Rename multiple files, groups of files
        1) rename -- Debian version and RedHat version differ, q.v.
           (try package util-linux:
           http://en.wikipedia.org/wiki/Util-linux)

        2) ren -- RedHat relatives

        3) renameutils -- package contains qmv, imv, icp, qcp, and deurlname

        4) mved -- (circa 2006; good as of 2015.05), perl
           http://raf.org/mved/
           (An earlier shell version may be available.)

        5) rename -- perl builtin library routine (DIY)

        6) mmv -- move/rename/copy/append/link multiple files by wildcard patterns

        7) gprename - batch rename using a GUI

        8) krename - batch rename using a GUI

Best wishes ... cheers, drl
# 6  
Old 03-24-2016
Quote:
Originally Posted by nemesis911
Here is bash script to move those files and rename them

Code:
for i in $(ls); do mv $i ${i/${i:0:7}/server2}; done

It replaces the string between the 0th and the 7th character with "server2".
That is a very dangerous suggestion. Note that if a directory contains files such as:
Code:
-rw-r--r--  1 dwc  staff  1082 Mar 23 21:11 _x.txt
-rw-r--r--  1 dwc  staff  1099 Mar 24 10:09 out
-rw-r--r--  1 dwc  staff  2321 Mar 23 21:29 problem
-rwxr-xr-x  1 dwc  staff   806 Mar 24 10:09 tester
-rw-r--r--  1 dec  staff  1082 Mar 24 10:10 x.txt

in addition to files with names starting with the string server1, it will execute commands like:
Code:
mv _x.txt server2
mv out server2
mv problem server2
mv tester server2
mv x.txt server2

throwing away the contents of 4 our of 5 of those files.

A much safer (and, since there is no need to invoke ls, faster) way to do this would be:
Code:
for i in server1*; do printf '%s %s %s\n' mv "$i" "${i/1/2}"; done

to verify that it is producing the mv commands you want and then rerun it with the printf and its format string shown in red removed after you have verified that it will be processing the files you want it to move correctly.
# 7  
Old 03-25-2016
Code:
for i in server1*
do 
     [ "$i" = "server1*" ] && continue # there wasn't any files
     # remove echo if looks good
      echo mv "$i" "${i/server1/server2}"
done


Last edited by kshji; 03-26-2016 at 05:14 AM.. Reason: Mistake, sorrry Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

parsing data from a big file using keys from another smaller file

Hi, I have 2 files format of file 1 is: a1 b2 a2 c2 d1 f3 format of file 2 is (tab delimited): a1 1.2 0.5 0.06 0.7 0.9 1 0.023 a3 0.91 0.007 0.12 0.34 0.45 1 0.7 a2 1.05 2.3 0.25 1 0.9 0.3 0.091 b1 1 5.4 0.3 9.2 0.3 0.2 0.1 b2 3 5 7 0.9 1 9 0 1 b3 0.001 1 2.3 4.6 8.9 10 0 1 0... (10 Replies)
Discussion started by: Lucky Ali
10 Replies

2. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

3. Shell Programming and Scripting

Parsing file, yaml file? Extracting specific sections

Here is a data file, which I believe is in YAML. I am trying to retrieve just the 'addon_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in awk and mostly bash scripting instead of perl... (3 Replies)
Discussion started by: Rhije
3 Replies

4. UNIX for Dummies Questions & Answers

Script for parsing details in a log file to a seperate file

Hi Experts, Im a new bee for scripting, I would ned to do the following via linux shell scripting, I have an application which throws a log file, on each action of a particular work with the application, as sson as the action is done, the log file would vanish or stops updating there, the... (2 Replies)
Discussion started by: pingnagan
2 Replies

5. Shell Programming and Scripting

File Parsing Help

Hello, I have a file which contains groups of fields. These groups are separated by a blank line, to form a logical record. Each line consists of a field-value pair. If want to find all records where field 'd' has a value of '4' and if it does, I want the value of field 'a' (from the... (4 Replies)
Discussion started by: brawnr
4 Replies

6. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

7. Shell Programming and Scripting

need help in Parsing a CSV file and generate a new output file

Hi Scripting Gurus, I am trying to parse a csv file and generate a new output file. The input file will be a variable length in turns of rows and columns. output file will have 8 columns. we have three columns from the header for each set. just to give little bit more clarification each row... (15 Replies)
Discussion started by: vkr
15 Replies

8. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies

9. Shell Programming and Scripting

Help me with parsing this file

Hi, I need a shell script that would parse this file /usr/share/i18n/locales/aa_DJ:title "Afar language locale for Djibouti (Cadu/Laaqo Dialects)." /usr/share/i18n/locales/aa_ER:title "Afar language locale for Eritrea (Cadu/Laaqo Dialects)." /usr/share/i18n/locales/aa_ER@saaho:title... (2 Replies)
Discussion started by: eamani_sun
2 Replies

10. Shell Programming and Scripting

Parsing a file

Hi, Please help in parsing the following file and write separate files by parsing the file for the new file's content. Main File: ------------ BEGIN FileName: FirstFile.txt Content of the File Start AAAAAAAA BBBBBBB Content of the File End END BEGIN FileName: SecondFile.txt... (11 Replies)
Discussion started by: aol12123
11 Replies
Login or Register to Ask a Question