Sponsored Content
Top Forums Shell Programming and Scripting Grabbing fields without using external commands Post 303008142 by SkySmart on Monday 27th of November 2017 06:23:06 PM
Old 11-27-2017
Quote:
Originally Posted by Scott
If you change the read to read into an array, you can access each member of the array by index.
e.g.
Code:
unset line
while IFS=_ read -A line; do
  NF=$((${#line[@]}-1))
  echo ${line[(($NF))]} # last field
  echo ${line[(($NF-1))]} # second last field
done < file

You could combine those two echo lines inside the while-loop with:
Code:
echo ${line[((${#line[@]}-1))]} # last field
echo ${line[((${#line[@]}-2))]} # second last field

but it's a bit messier to look at.
this works for bash:
Code:
#!/bin/bash

DATA="bblah1_blah2_blah3_blah4_blah5
bblahA_blahB_blahC_blahD_blahE"

printf '%s\n' "${DATA}" | while IFS=_ read -a line; do
  NF=$((${#line[@]}-1))
  echo ${line[(($NF-1))]}
done

but does not work for for sh. i get the following error:

Code:
read: Illegal option -a

if i try with the original -A which you had, i still get:

Code:
read: Illegal option -A

i care about this because there are some old systems we have here that dont have bash. they just have sh. systems such as AIX, SunOS.

any suggestions on how to circumvent this?
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grabbing a value from an output file

I am executing a stored proc and sending the results in a log file. I then want to grab one result from the output parameters (bolded below, 2) so that I can store it in a variable which will then be called in another script. There are more details that get printed in the beginning of the log file,... (3 Replies)
Discussion started by: hern14
3 Replies

2. Shell Programming and Scripting

grabbing more than one argument in a loop

Hello all I hope someone can help me. I am trying to convert something I wrote in C to bash. But how do I go about reading more than one item at a time in a for loop? i have been using this format for the loops in the bash script i have been building. e.g. for word in `cat -s... (4 Replies)
Discussion started by: Intense4200
4 Replies

3. Shell Programming and Scripting

Grabbing info from Telnet

I have a process that is running locally on the machine. When you telnet to the process: telnet IP port, it automatically returns a string which shows the status of that process. Something like this: # telnet IP Port Trying 127.0.0.1... Connected to localhost (127.0.0.1). Escape... (3 Replies)
Discussion started by: skaptakalian
3 Replies

4. Shell Programming and Scripting

Grabbing Certain Fields

ok, so a script i wrote spits out an output like the below: 2,JABABA,BV=114,CV=1,DF=-113,PCT=99.1228% as you can see, each field is separated by a comma. now, how can I get rid of the first field and ONLY show the rest of the fields. meaning, i want to get rid of the "2,", and... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Web Development

cgi script and external UNIX commands (like swadm)

Hi, I am trying to implement a server monitoring dashboard using cgi scripting. I am planning to run the necessary unix scripts from the web page using cgi. This method works fine for standard unix commands but I am unable to run some external unix commands (like swadm show_processes, swadm... (9 Replies)
Discussion started by: jofinjoseph
9 Replies

6. Shell Programming and Scripting

[Solved] Value of a variable is not recognised for commands comes from external file

Hi, my script is setting a variable with value and this variable is present in my another command that is coming from external file and this command is internally called after this variable is set. but while execution of this command, the value is not retrieved properly. say, my script... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

Grabbing strings with awk

Hello everyone, I am doing some sort of analysis for some data about organic solvents, and I have a problem with writing a command to do this: Here's a sample of my file: 1 ethanol 2 methanol 3 methanol/ethanol 4 ethanol/methanol 5 ethanol/DMF 6 ethyl... (6 Replies)
Discussion started by: Error404
6 Replies

8. Shell Programming and Scripting

Grabbing fields with perl

I have several .csv files containing data like this: field_1;field_2;date;comment;amount; I want to extract the 3 last fields and load them in a database. my input_file = "/dir/file.csv"; my output_file = "/dir/file.sql"; open my $csv_file, '<', $input_file or die "Can't... (1 Reply)
Discussion started by: freddie50
1 Replies

9. Shell Programming and Scripting

Grabbing variabes from logs

Hey guys, I am working on a script that needs to grab variables from a log file. The script will run morning 9 to 5pm and save variables for each run every hour, these results I will be aggregating at the end of the day. I am thinking I will be placing the date on each entry in the log every... (7 Replies)
Discussion started by: mo_VERTICASQL
7 Replies
ppmtosixel(1)						      General Commands Manual						     ppmtosixel(1)

NAME
ppmtosixel - convert a portable pixmap into DEC sixel format SYNOPSIS
ppmtosixel [-raw] [-margin] [ppmfile] DESCRIPTION
Reads a portable pixmap as input. Produces sixel commands (SIX) as output. The output is formatted for color printing, e.g. for a DEC LJ250 color inkjet printer. If RGB values from the PPM file do not have maxval=100, the RGB values are rescaled. A printer control header and a color assignment table begin the SIX file. Image data is written in a compressed format by default. A printer control footer ends the image file. OPTIONS
-raw If specified, each pixel will be explicitly described in the image file. If -raw is not specified, output will default to com- pressed format in which identical adjacent pixels are replaced by "repeat pixel" commands. A raw file is often an order of magni- tude larger than a compressed file and prints much slower. -margin If -margin is not specified, the image will be start at the left margin (of the window, paper, or whatever). If -margin is speci- fied, a 1.5 inch left margin will offset the image. PRINTING
Generally, sixel files must reach the printer unfiltered. Use the lpr -x option or cat filename > /dev/tty0?. BUGS
Upon rescaling, truncation of the least significant bits of RGB values may result in poor color conversion. If the original PPM maxval was greater than 100, rescaling also reduces the image depth. While the actual RGB values from the ppm file are more or less retained, the color palette of the LJ250 may not match the colors on your screen. This seems to be a printer limitation. SEE ALSO
ppm(5) AUTHOR
Copyright (C) 1991 by Rick Vinci. 26 April 1991 ppmtosixel(1)
All times are GMT -4. The time now is 02:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy