Accessing entire line during "while read"

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing entire line during "while read"
# 1  
Old 07-21-2009
Question Accessing entire line during "while read"

I am using the time-honored construct "while read field1 field2 field3 ; do" to pull delimited fields from lines of data in a file, but there are a few places within the loop where I need to manipulate the entire, unsplit line of data. Does "while read" keep each entire record/line somewhere prior to its separating the fields -- perhaps in some special variable like $? or $0?

If not, I would either have to use "while read entireLine ; do" and then split up the fields inside the loop with cut or awk, or, use "while read var1 var2 etc. ; do" and paste the separate fields back together where a copy of the whole line is needed, neither of which is very elegant.

Suggestions as to the prettiest, most elegant, or most standard way of having my cake and eating the slices too are most appreciated. --JMF
# 2  
Old 07-21-2009
You have to read the entire line first and then split it,
but you don't need external commands for that.
If your shell splits by default (like most shells do,
not like the Z-Shell), you could do something like this:

% bash -c '
  while IFS= read -r; do
    printf "entire record: %s\n" "$REPLY"
    set -- $REPLY
    printf "first word: %s, second word: %s\n" "$1" "$2"
  done<<<"a b"
entire record: a b
first word: a, second word: b

# 3  
Old 07-21-2009
Today has been
Example how to put line to array.

One line parsing, example 3 methods, same result.
If delimeter is something else as "whitespace", then change IFS, scipt include example,
if you need use | delimeter.

while read line
        echo "1___________"
        #IFS="|"   # set new deli
        #IFS="$OldIFS"  # return org deli 
        echo "${arr[*]}"
        echo "${#arr[*]}"
        echo "2___________"
        set -A arr -- $line
        echo "${arr[*]}"
        echo "${#arr[*]}"
        # radoulov example
        echo "3___________"
        set -- $line
        echo "$@"
        echo "$#"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

4. Shell Programming and Scripting

speeding up bash script with "while read line"

Hello everybody, I'm still slowly treading my way into bash scripting (without any prior programming experience) and hence my code is mostly what some might call "creative" if they meant well :D I have created a script that serves its purpose but it does so very slowly, since it needs to work... (4 Replies)
Discussion started by: origamisven
4 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

print "*" entire line

Hi folks, I want to display the "*" character the entire line. Instead of manually giving "*" multiple times in echo/print command, i want some short cut. i remember i have came across a straight forward method to display particular character (for example "*" ) for n number of times in unix.... (6 Replies)
Discussion started by: apsprabhu
6 Replies

7. Shell Programming and Scripting

defining a variable using multiple "entries/columns" within a line using read

Hello All, I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this: 1 2 3 4 5 6 7 8 9 where each number represents an entry of various characters deliminated by tabs. My goal is to set entries 2-7 as a variable/string that I... (3 Replies)
Discussion started by: Torinator
3 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. Shell Programming and Scripting

script to read a line with spaces bet " " and write to a file

Hi, I need a command in UNIX KSH below is the description... MAPPING DESCRIPTION ="Test Mapping for the calid inputs" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1" unix ksh command to read the DESCRIPTION and write to a file Test Mapping for the calid inputs... (3 Replies)
Discussion started by: perlamohan
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep ""|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep ""|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question