Unix Shell Novice - File Manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Shell Novice - File Manipulation
# 1  
Old 08-13-2006
Unix Shell Novice - File Manipulation

Hi,

I am brand new to unix and am hoping someone can start me in the right direction.
I want to be able to put the results of a file command such as wc -l filename into a variable which I can then use to test against another variable...i.e. I need to show the nth line of a file, but need to determine if the file contains at least n lines. currently the output from wc -l filename gives me a return of n filename - all I want is n as a numerical value.

Secondly, is there a way to retrieve the nth line of a file using a single command (similar to head or tail, but actually giving the line number?)

If anyone can help it would be appreciated.
# 2  
Old 08-13-2006
Suppose you want to check a file if it contains 15 or more lines and print the 12th line:
Code:
#! /bin/ksh

a=`wc -l filename | awk '{print $1}'`
if [[ $a -gt 15 ]]; then
sed -n '13q;12p' filename
fi

EDIT---
One other way can be:
You want to print nth line of a file
Code:
n=11
head +"$n" filename | tail -1


Last edited by tayyabq8; 08-13-2006 at 06:03 AM..
# 3  
Old 08-13-2006
Yet another way :
Code:
n=11
awk "NR==$n {print;exit}" filename


Jean-Pierre.
# 4  
Old 08-13-2006
Code:
n=11
sed -n "$n p" filename

For shell programming look at http://www.tldp.org/LDP/abs/html/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Manipulation - UNIX script

Hi, I have a file about 100 lines. Each line is about 2000 characters (each line is fixed length). In middle of each line is following constant value 0000040029892586 Now, I want to go through each line and increment by 1. So, line 1 will have 586 line 2 will have 587, line 3 will have... (4 Replies)
Discussion started by: jakSun8
4 Replies

2. Shell Programming and Scripting

Stream manipulation in UNIX shell scripting

i have a file something like this : start: 01:00:00 01:30:00 02:30:00 05:30:00 end: 01:13:00 02:00:00 02:40:00 05:45:00 and i want (end - start) total run time in below format: run: 00:13:00 00:30:00 00:10:00 00:15:00 (4 Replies)
Discussion started by: Acme
4 Replies

3. Shell Programming and Scripting

Manipulation of file data with UNIX

Hello , How all doing today.. I have a little doubt in Unix (6 Replies)
Discussion started by: adisky123
6 Replies

4. Shell Programming and Scripting

Novice in shell scripting - generating report

Hi I recently joined a project where I have been asked to generate a report using shell script accessing UNIX box. I have no idea on how to do it as I am a beginner and learning shell scripts. Suppose I have a XML: <XYZRequest> <effectiveDate>someDate</effectiveDate>... (2 Replies)
Discussion started by: vat1kor
2 Replies

5. AIX

Unix File name manipulation

I need a script that will raname the following file names that beging with 08078* in unix as follows: Rename 08078-08201103-H00044-CA.835 as follows: 08078-110820-H000440CA.835 Bascially it will do this: 1) Keep the first 6 positons. 2) Move the yr from the file name to be the... (4 Replies)
Discussion started by: mrn6430
4 Replies

6. UNIX Desktop Questions & Answers

PC User/UNIX Novice - Where do I start?

Hi all, I am new to the forum and this is my first post. I am an IT professional within a prodominently Windows Environment :D. I would like to learn the basics of UNIX/Solaris/Red Hat etc to create a niche skill on my CV. How do I go about it? -Which OS do I download and from where?... (6 Replies)
Discussion started by: mightymo26
6 Replies

7. Shell Programming and Scripting

Shell script text file manipulation.

Hello, I have mysql binary file which logs all the database queries and i to insert all queries log in to database. First i coverted binary file to text file. and start playing with it. Text file contains following queries, some samples are, SET INSERT_ID=1; INSERT INTO test... (0 Replies)
Discussion started by: mirfan
0 Replies

8. UNIX for Dummies Questions & Answers

UNIX - File/Table/Data manipulation

Hi, I have a table (e.g.): a 1 e 4 5 6 b 2 r 4 4 2 c 5 r 3 7 1 d 9 t 4 4 9 . . What I need to do is to set the values of some values in column 2 to negative values. For example, the values 2 and 9 should become -2 and -9 in the modified file. How should I go about... (2 Replies)
Discussion started by: pc2001
2 Replies

9. UNIX for Dummies Questions & Answers

NEW to Unix (novice)

Heya all Im just reading up on the solaris o/s and unix and i just have the following qustions 1) is the solaris o/s the same as Unix if not how are they different - i.e. are they different operating systems? 2) Can the Unix be loaded from CD without affectin windows o/s just like linux... (2 Replies)
Discussion started by: new214
2 Replies

10. Shell Programming and Scripting

Unix scripting-Need help-NOVICE -PLEASE HELP

I really want to get into unix scripting,work with RS6000 -AIX. How do i get started,what books are good for beginners,i am very desperate I have no programming background but ready to scrafice all my time in learning .please help. PLEASE,PLEASE PLEASE ,HELP.... Any advice will realy... (2 Replies)
Discussion started by: Ghanaman
2 Replies
Login or Register to Ask a Question