LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-17-2005, 07:03 PM   #1
izza_azhar
LQ Newbie
 
Registered: Jan 2005
Location: malaysia
Posts: 18

Rep: Reputation: 0
split string


if file name such:
ATK_7B1_WIP_20041116160700.9.xml.dat
ATK_7B1_WIP_20041116160700.93474039283928384.xml.dat
so how can i cut to only get the year,month,day and year and month

such:20041116 or 200411
 
Old 01-17-2005, 08:29 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
This works for me (assuming the files have all YYYYMMDD format for the date - as in 01 for January, 02 for February, etc.)
Code:
for entry in $( ls ATK* | cut -f 4 -d '_' ) ; do echo ${entry:0:8}; done
 
Old 01-17-2005, 08:46 PM   #3
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Or using Sed:
Code:
echo Please enter output filename:
read filename

touch $filename
for f in *.xml.dat; do
        echo Processing $f
        echo $f | sed 's/ATK_7B1_WIP_//' | head -c8 >> $filename
        echo $CHAR112 >> $filename
done

echo
echo Done !
Not as simple or elegent as Dark_Helmet's code (or as portable for that matter), but it works for me.

Cheers,

mj
 
Old 01-17-2005, 09:14 PM   #4
izza_azhar
LQ Newbie
 
Registered: Jan 2005
Location: malaysia
Posts: 18

Original Poster
Rep: Reputation: 0
let say...

if my program such this:

for FILE in ls ATK*.dat
do
curr_year=$(date +%Y)
curr_month=$(date +%Y%m)
file_year=<filename just have the year>
file_month=<filename that just have year and month>

if [ "$curr_year" = "$file_year" ]
then
if [ 'expr $curr_month - $file_month >= 3 ]
then
mv $FILE /u006/r45647/Izza/xsltOutput/archiveprocess
<move the file to another directory>

fi

fi

done

can we use sen to pass the variable for $file_year or file month?
or it's easy use the cut only?
 
Old 01-18-2005, 03:21 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
Code:
billym.primadtpdev>cat ~/1

for file in *.dat;do
     x=${file##*_}
    date=${x%%.*}
    echo "[$date] $file"

done
Code:
billym.primadtpdev>bash ~/1
[20041116160700] ATK_7B1_WIP_20041116160700.9.xml.dat
[20041116160700] ATK_7B1_WIP_20041116160700.93474039283928384.xml.dat
you can then use touch -t to set the time
and then find -ctime +<no_of_days> to find
the overdue files.
 
Old 01-18-2005, 11:48 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Re: split string

Quote:
Originally posted by izza_azhar
if file name such:
ATK_7B1_WIP_20041116160700.9.xml.dat
ATK_7B1_WIP_20041116160700.93474039283928384.xml.dat
so how can i cut to only get the year,month,day and year and month

such:20041116 or 200411
Like is posted in your other thread on this subject:
Code:
for FILENAME in ATK_*.dat ; do
	FILEDATE=$(echo $FILENAME | cut -c 13-20)
	# ....
done
 
Old 01-18-2005, 08:24 PM   #7
izza_azhar
LQ Newbie
 
Registered: Jan 2005
Location: malaysia
Posts: 18

Original Poster
Rep: Reputation: 0
Talking thanks

it's really work
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 01:58 PM
split string prob izza_azhar Programming 3 02-08-2005 12:11 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Need to split an input string general4172 Linux - Software 6 10-29-2003 11:57 PM
perl string split problem! farhanali Programming 4 06-22-2003 07:08 AM


All times are GMT -5. The time now is 05:26 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration