Grabbing top line of text in a file and setting it to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grabbing top line of text in a file and setting it to a variable
# 1  
Old 04-16-2012
Grabbing top line of text in a file and setting it to a variable

If I have a txt file with

test.txt
Code:
somelineoftext

and I want to set that line of text to variable in a script:

so
Code:
#!/bin/bash
var=''

becomes
Code:
#!/bin/bash
var='somelineoftext'

Im sure I use grep but I don't know what the command would be????

Is there a way of doing that?
# 2  
Old 04-16-2012
Code:
var=`head -n 1 test.txt`

# 3  
Old 04-16-2012
What if it was the second line in the text file is that 2?
# 4  
Old 04-16-2012
2 will print two lines from top of file. that is not what you need right.

use either of these
Code:
var=`head -n 2 | tail -n 1 test.txt`

Code:
var=`awk 'NR==2' test.txt`

Code:
var=`sed -n '2,2p' test.txt`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending a text to the top of each line

Platform : Oracle Linux 6.8 Shell : bash I have a file which has lines like below. These are SELECT queries (SQL) In each line, I want the word just after FROM keyword to be copied and printed on the top along with the word PROMPT. The words after FROM clause below are table names. So, they... (6 Replies)
Discussion started by: John K
6 Replies

2. UNIX for Dummies Questions & Answers

[Help] Adding text to a variable line in a file

Hey guys, I need to write a script that will add a specific text at the end of a specific line (of a text file). but the line is a variable this is my text file : device_2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=30000 ansible_ssh_user='root' device_2 ansible_ssh_host=127.0.0.1... (1 Reply)
Discussion started by: OdedOvdat
1 Replies

3. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

4. Shell Programming and Scripting

Grabbing data between 2 points in text file

I have a text file that shows the output of my solar inverters. I want to separate this into sections. overview , device 1 , device 2 , device 3. Each device has different number of lines. but they all have unique starting points. Overview starts with 6 #'s, Devices have 4#'s and their data starts... (6 Replies)
Discussion started by: Mikey
6 Replies

5. Shell Programming and Scripting

Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it. I cannot get one of the variables to display back in the main calling script... (2 Replies)
Discussion started by: scottrif
2 Replies

6. Shell Programming and Scripting

EXPECT: Assign variable by reading a line of text from a file

Hi All, I have been using a program on windows called AutoKey. My environment at work is Linux and I have been experimenting with expect. Very powerful. I can move my AutoKey scripts to Linux using Expect once I am educated on how to read from a file using Expect. My application would be... (1 Reply)
Discussion started by: quemalr
1 Replies

7. Shell Programming and Scripting

Grabbing a chunk of text from a file

Hi, I have a Report.txt file. Say the contents of this file are : 1 2 3 4 5 7 df v g gf e r dfkf lsdk dslsdklsdk Report Start: xxxxxxdad asdffsdfsdfsdfasfasdffasdf sadfasdfsadffsfsdf Report End. sdfasdfasdf sdfasfdasdfasdfasdfasdf sadfasdfsdf I need to grab from Report Start... (3 Replies)
Discussion started by: mrskittles99
3 Replies

8. Shell Programming and Scripting

Grabbing text and using that text in a newly created line

Hello, I am really stuck and I'm hoping somone can help. I have a text file that is similar to this: <--First User--> <function>account='uid=user1,....... <--Second User--> <function>account='uid=user2,.......What I want is to grab the usernames after "uid=" and before the following... (9 Replies)
Discussion started by: mafia910
9 Replies

9. Shell Programming and Scripting

grabbing filename from text file....should be easy!

Quick question...I'm trying to grab the .tif file name from this output from our fax server. What is the best way i can do this in a bash script? I have been looking at regular expressions with bash or using awk but having some trouble. thanks! The only output i want is... (5 Replies)
Discussion started by: kuliksco
5 Replies

10. Shell Programming and Scripting

Sed grabbing the last line of a file

I can grab the first line w/ sed how do you grab the last line of a file? (2 Replies)
Discussion started by: xgringo
2 Replies
Login or Register to Ask a Question