PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How to check column contains substring and another column starts with substring

 August 28, 2022     batch-file, cmd, csv, for-loop     No comments   

Issue

This is my first time working with batch files. I am trying to extract certain columns from original csv and pipe output to new csv. The following code is what I wrote based on this link:

https://stackoverflow.com/a/17557532/16034206

@echo off 
setlocal EnableDelayedExpansion

Rem for /f "skip=1 usebackq tokens=1,2,10,11 delims=," %%i in (sample.csv) do @echo %%i,%%j,%%k,%%l >>output.csv
echo "Your script is starting..."

FOR /F "skip=1 usebackq delims=" %%L in (sample.csv) DO (
    set "line=%%L,,,,,,,,"
    set "line=#!line:,=,#!"
    FOR /F "tokens=1,2,10,11 delims=," %%a in ("!line!") DO (
        set "param1=%%a"
        set "param2=%%b"
        set "param10=%%c"
        set "param11=%%d"
        set "param1=!param1:~1!"
        set "param2=!param2:~1!"
        set "param10=!param10:~1!"
        set "param11=!param11:~1!"
        if "%%~A"=="RH" echo !param1!, !param2!, !param10!, !param11! >> output.csv
    )
)

echo "Your script has completed"

I am looking to apply logic to check param1 contains a substring "@gmail.com" AND that param10 starts with a specific string "100" before outputting that specific row of 4 columns into the csv.

I checked how to use if-statement from this link: https://stackoverflow.com/a/17474377/10671013 but I have not found any links on SO discussing "containing substring" or checking for "starting with a string". Please advise.


Solution

Remove the substring you look for from the first column and compare it with the original string, if not equal (string contains substring), check the first three characters of the other column. (This substring substitution is case insensitive):

if not "!param1:@gmail.com=!" == "!param1!" if  "!param10:~0,3!" == "100" echo ...


Answered By - Stephan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing