Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:52:12+00:00 2026-05-27T09:52:12+00:00

I have a csv file like, let’s say table.csv foo;3 bar;1 and a script

  • 0

I have a csv file like, let’s say table.csv

foo;3
bar;1

and a script

#!/bin/bash
[..]
# increment ${searchterm} by one
eval [..]
# decrement ${searchterm} by one

and my problem is to replace the right line and value with the incremented integer. In addition: If the searchterm is not in the file, it should be assumed as 0.

The goal is to have a machine readable overview of the number of processes in different the eval-statements.

awk or sed might be solutions but I didn’t find a clue.

Edit: Hi, as @shellter’s answer already solved my problem I only try to get it more precise for documentation.
With the input “foo” I wanted to increment to

foo;4
bar;1

and after running through the eval-statement get back to the original values.
With the input of “demo” I want to get an output like

foo;3
bar;1
demo;1

and afterwards back to

foo;3
bar;1
demo;0

afterwords.

The idea behind is to have the whole script running in several instances at the same time and to be able to read the number of instances from the table.csv.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:52:12+00:00Added an answer on May 27, 2026 at 9:52 am

    given your data, and your sparse description (it is always good to show the output you need/expect AND and any error messages you are getting), try this

    cat myData
    foo;3
    bar;1
    
    cat myFixer.sh
    #!/bin/bash
    awk '
      BEGIN {FS=";" ; OFS=";"}
      {
         if ($1 == target ) {
           $2 += incr
           targFound=1
         }
         print $0
      }
      END { 
        if (! ( targFound) ) {
          print target OFS "0"
        }
      }'  target="foo" incr=3 myData
    
     # output
     foo;6
     bar;1
    
     foo;3
     bar;1
     BAD;0
    

    I use incr=3 to show that this is a generic solution. If you need a +1 increment, just change the argment to incr=1.

    Some awks want to see the command-line variables set as -v target=foo -v incr=3.

    You can easily change this bash script to accept arguments for the target and incr, input file as $1 $2 $3.

    Right now the output just appears on your screen. I’d recommend that you redirect output to a new file.

    myFixer.sh > myData.New 
    

    Note that FS is the awk special var that means FieldSeparator. OFS is OutputFieldSeparator, so it is possible to easily convert to true CSV in your case by changing OFS to OFS=","

    Awk parses each line of input into fields; the whole line is refered to as $0, while each element as delimited by FS (in your case ‘;’), is assigned a sequential number moving from left to right, so the first field is called $1, 2nd is $2, etc.. Awk has many other internal variables including NF (Number_of_Fields), which is set for each line of data read. You can use NF by itself to validate you have the correct number of Fields in a line (for one example) or you can ‘dereference’ the value of NF with the ‘$’ char and have access to the last value on each line with ‘$NF’. For you example, we could rewrite $2 as $NF.

    The default action for awk, given code in { .. } blocks, is to read each line of input, and then process it as dictated by the logic inside the block. In your case, we look at field one to see if it is the target string, and if it is, we increment the value there.

    For all records, we print out the complete line, whether it has been incremented or not.

    A BEGIN block of code is executed before any records from the input files are read, and is a way for variable initializations to happen (or in this case, you are overriding the default value of FS).

    A END block is executed after all input files are read. As you need to print foo;0 if it is not there, we keep a flag to indicate whether your search target has been found or not. The last thing we do is check to see if target was found and if not, emit a line with ‘target OFS 0’ as text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a dictionary of names (a huge CSV file). I want
Ok, I have a csv file like this: 14 ; 1234,56 ; 10203 ;
I have a CSV file formatted just like this: name,color,tasty,qty apple,red,true,3 orange,orange,false,4 pear,greenish-yellowish,true,1 As
I have a CSV file that is formatted like: 0.0023709,8.5752e-007,4.847e-008 and I would like
I have various time-series I'd like to correlate and present as either a csv-file
Let's say I have a data.frame like: a <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10) df <- data.frame(a,rnorm(100)) And
Let's say I have a database like this: Users ----- ID int PK Identity
I have a .csv file like this: stack2@domain.example,2009-11-27 01:05:47.893000000,domain.example,127.0.0.1 overflow@domain2.example,2009-11-27 00:58:29.793000000,domain2.example,255.255.255.0 overflow@domain2.example,2009-11-27 00:58:29.646465785,domain2.example,256.255.255.0 ...
I have some lines in a CSV file like this: 1000001234,Account Name,0,0,3,711.32,0,0,18,629.64,22,340.96,COD,20,000.00,Some string,Some string
I have csv file with a line that looks something like this: ,,,,,,,,,, That's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.