Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Systems > Linux / BSD / OS X
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 01-15-2006, 07:58 PM   #1 (permalink)
ashok rajagopal
Registered User
 
ashok rajagopal's Avatar
 
Join Date: Sep 2005
Posts: 20
ashok rajagopal is on a distinguished road
File handling in Shell Script

In my application, i have to create a shell script which takes in a data file and gets some data from it. Basically, i need to read n number of characters from each record in the file into a variable, something like 'fread' command in C/C++... which is the unix command or construct that i can use ?

I have to write some values to another file, and i want the command for writing a variable to the file also. like 'fprintf' in C/C++ . Any pointers ???

Want immediate help on this... Please...
__________________
ashok rajagopal is offline   Reply With Quote
Old 01-16-2006, 07:23 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
Something like this:
Code:
#!/bin/sh if [ -z $1 ]; then echo "Usage: $0 <file> <n>" exit fi if [ -z $2 ]; then echo "Usage: $0 <file> <n>" exit fi if [ ! -f $1 ]; then echo "File: $1 isn't a file" exit fi for n in `grep -n $ $1 | awk -F ':' '{print $1}'`; do LINE=`head -$n $1|tail -1` # fetch the first n chars from line CHARS=`echo $LINE | cut -c 0-$2` # do whatever with CHARS echo $CHARS done
Remember you can form your echo statement any which way you want it to be displayed, and you can use awk/cut to pick anything out of your $LINE
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 01-17-2006, 01:26 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
The head/tail way is really the only way to do it with a conventional shell script. You can do shell-scripting with Perl and PHP, you might want consider using one of those if you're going to be parsing a file. I combine both shell-scripts and PHP scripts for my system-admin work.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script variable sde Linux / BSD / OS X 9 12-13-2005 06:59 PM
What is a shell variable in UNIX Shell Script rakesh Linux 0 08-31-2004 08:25 AM
.htaccess -- Image Hotlinking Prevention DavH27 HTML / CSS 0 08-27-2004 03:43 AM
.htaccess -- Custom Error Pages v1.1 DavH27 HTML / CSS 0 08-26-2004 04:40 PM
shell script for ftp Hrqls Linux / BSD / OS X 7 09-20-2002 03:14 AM


All times are GMT -8. The time now is 02:10 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle