- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- checking for the first line of the file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2006 02:43 PM
07-12-2006 02:43 PM
checking for the first line of the file
I would like to ask how would i check the content of first line the file (grep?). Normally, the first line of the file would be
Test Center, XXXXX XXXX XXXX XXXX
...
...
However, some file have different pattern. So what i would like to do is check first the first line of the file if it contains the Test Center word and do nothing. If the first line does not contain the word Test Center, then it will be move to a temp directory.
Maximum point for all correct replies!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2006 02:58 PM
07-12-2006 02:58 PM
Re: checking for the first line of the file
# head -n 1 source_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2006 03:23 PM
07-12-2006 03:23 PM
Re: checking for the first line of the file
I think we'll use read
#!/usr/bin/sh
typeset MYFILE=""
typeset X=""
typeset -i STAT=0
typeset -i GREPSTAT=0
MYFILE=/xxx/yyy/myfile.txt
if [[ -r "${MYFILE}" ]]
then
read X < "${MYFILE}"
echo "${X}" | grep -q -i "Test Center"
GREPSTAT=${?}
if [[ ${GREPSTAT} -eq 0 ]]
then
echo "File should be moved"
else
echo "No match"
fi
else
echo "File ${MYFILE} not found" >&2
STAT=1
fi
exit ${STAT}
------------------
read when used like this will read 1 line from stdin. Note the use of quotes. Grep -q is used because we are only interested in the exit status (${?})of the command. 0 => match.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2006 03:39 PM
07-12-2006 03:39 PM
Re: checking for the first line of the file
#!/bin/sh
for i in $(ls -1
do
awk 'NR==1 && $0!~/^Test Center,/ {system("mv "FILENAME" /tmp")}' $i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2006 03:41 PM
07-12-2006 03:41 PM
Re: checking for the first line of the file
perl -ne 'exit ( /^Test Center,/' file
now test status.
-n = loop through input
-e = perl script to follow
exit immediately on reading the first line with the status being the match condition.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2006 07:17 PM
07-12-2006 07:17 PM
Re: checking for the first line of the file
to add a sed solution:
for file in *
do
first=$(sed 1q $file)
case "$first" in
TestCenter,*) ;;
*) mv $file tempdir ;;
esac
done
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 02:56 AM
07-13-2006 02:56 AM
Re: checking for the first line of the file
perl -ne 'close ARGV; rename $ARGV, "temp" unless /^Test Center,/' file
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 07:41 AM
07-13-2006 07:41 AM
Re: checking for the first line of the file
#!/bin/sh
cd /place_where_the_files_live
for file in `ls` ; do
mytest=`head -1 $file|grep "Test Center"`
if [ -z $mytest ]
then
mv $file /temp_directory
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 07:46 AM
07-13-2006 07:46 AM
Re: checking for the first line of the file
#!/bin/sh
cd /place_where_the_files_live
for file in `ls` ; do
mytest=`head -1 $file|grep "Test Center"`
if [ -z "$mytest" ]
then
mv $file /temp_directory
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 09:44 AM
07-13-2006 09:44 AM
Re: checking for the first line of the file
if [ $? -eq 0 ]
then
exit 99
else
put code here you want to do
fi
exit 0
Rgds...Geoff