LinuxQuestions.org
Review your favorite Linux distribution.
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 05-19-2006, 04:54 AM   #1
shmuel_orgad
LQ Newbie
 
Registered: May 2006
Posts: 5

Rep: Reputation: 0
Shell scripts


Hello

I have a few questions about writing scripts.

1. Who do I change a file into a string. My file contain fields like " inr05, inr02, ipr05 ". What is the syntax ?

2. my file contain fields like " inr05, inr02, ipr05 ". Who can I sort the file according to the last 2 characters ?

3. What is the syntax for the command " foreach " for a file. I know the syntax for a string

foreach lay ($string)
commands..
end

Thank a lot

Shmuel Orgad
 
Old 05-19-2006, 12:37 PM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
hi there,
show us what you've tried so far...
regards,
slackie1000
 
Old 05-19-2006, 09:09 PM   #3
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,215

Rep: Reputation: 56
To put the contents of a file into a string use the cat command and a variable, something like this:


#!/bin/sh
FILE1=`cat myfile.txt`
echo $FILE1

This will display the contents of the file to the screen, then you can use other commands like sed, grep and awk depending on what you want to do with the variable. If you are wanting to find fields like `inr05`, use grep and awk:

#!/bin/sh
FILE1=`cat myfile.txt | grep "inr05" | awk '{print $1}'`


You really need to read up on bash scripting and also how to use grep. sed and awk very useful commands if your going to do any scripting
 
Old 05-20-2006, 11:28 AM   #4
shmuel_orgad
LQ Newbie
 
Registered: May 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by slackie1000
hi there,
show us what you've tried so far...
regards,
slackie1000
Hi

the file i have contain the following fildes"

record #1: ipr05
record #2: inr02
record #3: ipr04
record #1: ipr03

I need to sort this file by the 2 last digits, ( 05, 02, 04, 03)after sorting the file the order of the record should like this:

record #1: inr02
record #2: ipr03
record #3: ipr04
record #1: ipr05

What is the command and the syntax for it ?

Thank's a lot

Shmuel
 
Old 05-21-2006, 05:33 AM   #5
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
hi there,
i am sure this is homework. you also asked this question 4 times.
here is your solution with one command only
Code:
sort +2 $your_file
before asking again, please refer to
Code:
man sort
regards,
slackie1000
 
  


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
I Need shell scripts flyyy_82 Linux - Security 1 01-27-2005 04:05 PM
shell scripts rocketgo Linux - General 10 12-01-2003 05:20 AM
need help on shell scripts!!! krcool32 Programming 5 04-28-2003 03:08 PM
shell scripts nautilus_1987 Linux - General 3 08-30-2002 03:12 AM
shell scripts gui10 Programming 10 10-28-2001 02:46 AM


All times are GMT -5. The time now is 05:20 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